Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows to use type[T] in stubs #11863

Merged
merged 5 commits into from Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions mypy/typeanal.py
Expand Up @@ -163,6 +163,14 @@ def __init__(self,
# Names of type aliases encountered while analysing a type will be collected here.
self.aliases_used: Set[str] = set()

def is_future_annotations(self, target_version: Optional[Tuple[int, int]] = None) -> bool:
return (
(self.options.python_version >= target_version
if target_version is not None else False)
or self.api.is_future_flag_set('annotations')
or self.allow_new_syntax # basically tells us if this is a stub
)

def visit_unbound_type(self, t: UnboundType, defining_literal: bool = False) -> Type:
typ = self.visit_unbound_type_nonoptional(t, defining_literal)
if t.optional:
Expand Down Expand Up @@ -203,8 +211,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
return hook(AnalyzeTypeContext(t, t, self))
if (fullname in get_nongen_builtins(self.options.python_version)
and t.args and
not self.allow_new_syntax and
not self.api.is_future_flag_set("annotations")):
not self.is_future_annotations()):
self.fail(no_subscript_builtin_alias(fullname,
propose_alt=not self.defining_alias), t)
tvar_def = self.tvar_scope.get_binding(sym)
Expand Down Expand Up @@ -291,9 +298,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
" in a variable annotation", t)
return AnyType(TypeOfAny.from_error)
elif (fullname == 'typing.Tuple' or
(fullname == 'builtins.tuple' and (self.options.python_version >= (3, 9) or
self.api.is_future_flag_set('annotations') or
self.allow_new_syntax))):
(fullname == 'builtins.tuple' and self.is_future_annotations((3, 9)))):
# Tuple is special because it is involved in builtin import cycle
# and may be not ready when used.
sym = self.api.lookup_fully_qualified_or_none('builtins.tuple')
Expand Down Expand Up @@ -326,8 +331,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
elif fullname == 'typing.Callable':
return self.analyze_callable_type(t)
elif (fullname == 'typing.Type' or
(fullname == 'builtins.type' and (self.options.python_version >= (3, 9) or
self.api.is_future_flag_set('annotations')))):
(fullname == 'builtins.type' and self.is_future_annotations((3, 9)))):
if len(t.args) == 0:
if fullname == 'typing.Type':
any_type = self.get_omitted_any(t)
Expand Down Expand Up @@ -704,9 +708,7 @@ def visit_star_type(self, t: StarType) -> Type:
def visit_union_type(self, t: UnionType) -> Type:
if (t.uses_pep604_syntax is True
and t.is_evaluated is True
and self.api.is_stub_file is False
and self.options.python_version < (3, 10)
and self.api.is_future_flag_set('annotations') is False):
and not self.is_future_annotations((3, 10))):
self.fail("X | Y syntax for unions requires Python 3.10", t)
return UnionType(self.anal_array(t.items), t.line)

Expand Down
3 changes: 3 additions & 0 deletions test-data/unit/check-generic-alias.test
Expand Up @@ -273,6 +273,8 @@ b: B
import m
reveal_type(m.a) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(m.b) # N: Revealed type is "builtins.list[builtins.list[builtins.int]]"
m.C # has complex representation, ignored
reveal_type(m.d) # N: Revealed type is "Type[builtins.str]"

[file m.pyi]
A = list[int]
Expand All @@ -281,4 +283,5 @@ B = list[list[int]]
b: B
class C(list[int]):
pass
d: type[str]
[builtins fixtures/list.pyi]