Skip to content

Commit 811e248

Browse files
committed
Revert "Dedupe logic to see if new syntax is allowed"
This reverts commit 7281285.
1 parent 7281285 commit 811e248

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

mypy/semanal.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -482,19 +482,6 @@ def is_typeshed_stub_file(self) -> bool:
482482
def final_iteration(self) -> bool:
483483
return self._final_iteration
484484

485-
@property
486-
def always_allow_new_syntax(self) -> bool:
487-
return self.is_stub_file or self.is_future_flag_set("annotations")
488-
489-
def check_pep585_name(self, fullname: str, name: str) -> bool:
490-
return fullname == "typing." + name.capitalize() or (
491-
fullname == "builtins." + name.lower()
492-
and (
493-
self.always_allow_new_syntax
494-
or self.options.python_version >= (3, 9)
495-
)
496-
)
497-
498485
@contextmanager
499486
def allow_unbound_tvars_set(self) -> Iterator[None]:
500487
old = self.allow_unbound_tvars
@@ -1024,7 +1011,17 @@ def is_expected_self_type(self, typ: Type, is_classmethod: bool) -> bool:
10241011
if (
10251012
sym is not None
10261013
and typ.args
1027-
and self.check_pep585_name(sym.fullname or "", "type")
1014+
and (
1015+
sym.fullname == "typing.Type"
1016+
or (
1017+
sym.fullname == "builtins.type"
1018+
and (
1019+
self.is_stub_file
1020+
or self.is_future_flag_set("annotations")
1021+
or self.options.python_version >= (3, 9)
1022+
)
1023+
)
1024+
)
10281025
):
10291026
return self.is_expected_self_type(typ.args[0], is_classmethod=False)
10301027
return False

mypy/semanal_shared.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,6 @@ def is_future_flag_set(self, flag: str) -> bool:
128128
def is_stub_file(self) -> bool:
129129
raise NotImplementedError
130130

131-
@property
132-
@abstractmethod
133-
def always_allow_new_syntax(self) -> bool:
134-
"""Should we allow new type syntax when targeting older Python versions
135-
like 'list[int]' or 'X | Y' (allowed in stubs and with `__future__` import)?
136-
"""
137-
raise NotImplementedError
138-
139-
@abstractmethod
140-
def check_pep585_name(self, fullname: str, name: str) -> bool:
141-
raise NotImplementedError
142-
143131
@abstractmethod
144132
def is_func_scope(self) -> bool:
145133
raise NotImplementedError

mypy/typeanal.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ def __init__(
209209
self.allow_tuple_literal = allow_tuple_literal
210210
# Positive if we are analyzing arguments of another (outer) type
211211
self.nesting_level = 0
212+
# Should we allow new type syntax when targeting older Python versions
213+
# like 'list[int]' or 'X | Y' (allowed in stubs and with `__future__` import)?
214+
self.always_allow_new_syntax = self.api.is_stub_file or self.api.is_future_flag_set(
215+
"annotations"
216+
)
212217
# Should we accept unbound type variables? This is currently used for class bases,
213218
# and alias right hand sides (before they are analyzed as type aliases).
214219
self.allow_unbound_tvars = allow_unbound_tvars
@@ -292,7 +297,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
292297
if (
293298
fullname in get_nongen_builtins(self.options.python_version)
294299
and t.args
295-
and not self.api.always_allow_new_syntax
300+
and not self.always_allow_new_syntax
296301
):
297302
self.fail(
298303
no_subscript_builtin_alias(fullname, propose_alt=not self.defining_alias), t
@@ -509,7 +514,10 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
509514
code=codes.VALID_TYPE,
510515
)
511516
return AnyType(TypeOfAny.from_error)
512-
elif self.api.check_pep585_name(fullname, "tuple"):
517+
elif fullname == "typing.Tuple" or (
518+
fullname == "builtins.tuple"
519+
and (self.always_allow_new_syntax or self.options.python_version >= (3, 9))
520+
):
513521
# Tuple is special because it is involved in builtin import cycle
514522
# and may be not ready when used.
515523
sym = self.api.lookup_fully_qualified_or_none("builtins.tuple")
@@ -542,7 +550,10 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
542550
return make_optional_type(item)
543551
elif fullname == "typing.Callable":
544552
return self.analyze_callable_type(t)
545-
elif self.api.check_pep585_name(fullname, "type"):
553+
elif fullname == "typing.Type" or (
554+
fullname == "builtins.type"
555+
and (self.always_allow_new_syntax or self.options.python_version >= (3, 9))
556+
):
546557
if len(t.args) == 0:
547558
if fullname == "typing.Type":
548559
any_type = self.get_omitted_any(t)
@@ -1092,7 +1103,7 @@ def visit_union_type(self, t: UnionType) -> Type:
10921103
if (
10931104
t.uses_pep604_syntax is True
10941105
and t.is_evaluated is True
1095-
and not self.api.always_allow_new_syntax
1106+
and not self.always_allow_new_syntax
10961107
and not self.options.python_version >= (3, 10)
10971108
):
10981109
self.fail("X | Y syntax for unions requires Python 3.10", t, code=codes.SYNTAX)

0 commit comments

Comments
 (0)