|
364 | 364 | # string literal as a type expression. |
365 | 365 | _MULTIPLE_WORDS_NONTYPE_RE = re.compile(r'\s*[^\s.\'"|\[]+\s+[^\s.\'"|\[]') |
366 | 366 |
|
367 | | -# Matches any valid Python identifier, including identifiers with Unicode characters. |
368 | | -# |
369 | | -# [^\d\W] = word character that is not a digit |
370 | | -# \w = word character |
371 | | -# \Z = match end of string; does not allow a trailing \n, unlike $ |
372 | | -_IDENTIFIER_RE = re.compile(r"^[^\d\W]\w*\Z", re.UNICODE) |
373 | | - |
374 | 367 |
|
375 | 368 | class SemanticAnalyzer( |
376 | 369 | NodeVisitor[None], SemanticAnalyzerInterface, SemanticAnalyzerPluginInterface, SplittingVisitor |
@@ -8043,16 +8036,9 @@ def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> None: |
8043 | 8036 | return |
8044 | 8037 | elif isinstance(maybe_type_expr, StrExpr): |
8045 | 8038 | str_value = maybe_type_expr.value # cache |
8046 | | - # Filter out string literals with common patterns that could not |
8047 | | - # possibly be in a type expression |
8048 | | - if _MULTIPLE_WORDS_NONTYPE_RE.match(str_value): |
8049 | | - # A common pattern in string literals containing a sentence. |
8050 | | - # But cannot be a type expression. |
8051 | | - maybe_type_expr.as_type = None |
8052 | | - return |
8053 | 8039 | # Filter out string literals which look like an identifier but |
8054 | 8040 | # cannot be a type expression, for a few common reasons |
8055 | | - if _IDENTIFIER_RE.fullmatch(str_value): |
| 8041 | + if str_value.isidentifier(): |
8056 | 8042 | sym = self.lookup(str_value, UnboundType(str_value), suppress_errors=True) |
8057 | 8043 | if sym is None: |
8058 | 8044 | # Does not refer to anything in the local symbol table |
@@ -8078,13 +8064,21 @@ def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> None: |
8078 | 8064 | return |
8079 | 8065 | else: # does not look like an identifier |
8080 | 8066 | if '"' in str_value or "'" in str_value: |
8081 | | - # Only valid inside a Literal[...] type |
| 8067 | + # Only valid inside a Literal[...] or Annotated[..., ...] type |
8082 | 8068 | if "[" not in str_value: |
8083 | | - # Cannot be a Literal[...] type |
| 8069 | + # Cannot be a Literal[...] or Annotated[..., ...] type |
8084 | 8070 | maybe_type_expr.as_type = None |
8085 | 8071 | return |
8086 | | - elif str_value == "": |
8087 | | - # Empty string is not a valid type |
| 8072 | + elif len(str_value) < 2 or str_value.isspace(): |
| 8073 | + # Whitespace-only strings cannot be valid types. Very short strings can |
| 8074 | + # only be valid if they are identifiers, but we already checked for those. |
| 8075 | + maybe_type_expr.as_type = None |
| 8076 | + return |
| 8077 | + # Filter out string literals with common patterns that could not |
| 8078 | + # possibly be in a type expression |
| 8079 | + if _MULTIPLE_WORDS_NONTYPE_RE.match(str_value): |
| 8080 | + # A common pattern in string literals containing a sentence. |
| 8081 | + # But cannot be a type expression. |
8088 | 8082 | maybe_type_expr.as_type = None |
8089 | 8083 | return |
8090 | 8084 | elif isinstance(maybe_type_expr, IndexExpr): |
|