8000 Interpret `type` annotations as `type[Any]` annotations by tyralla · Pull Request #16366 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Interpret type annotations as type[Any] annotations #16366

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

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a430c12
Interpret `type` annotations as `type[Any]` annotations.
tyralla Oct 29, 2023
56fb2bd
Replace `type` with `type[object]` in Mypy's code.
tyralla Nov 13, 2023
fe0cd0a
Find other places where `analyze_annotation` should eventually be set…
tyralla Nov 15, 2023
28ee26b
* Rename `analyze_annotation` to `builtin_type_is_type_type` and mak…
tyralla Nov 21, 2023
84d06cb
Merge branch 'master' into fix/narrow_type_vs_Type
tyralla Nov 21, 2023
dcf1f95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2023
b1bb26c
small fixes
tyralla Nov 21, 2023
339f0b9
Merge branch 'fix/narrow_type_vs_Type' of https://github.com/tyralla/…
tyralla Nov 21, 2023
80a8c67
minor fixes after review
tyralla Nov 21, 2023
ab7322c
Add type[ignore] to tuple.pyi to satisfy testDisallowAnyExplicitGener…
tyralla Nov 21, 2023
aa0cd97
Add error codes to the type: ignores in tuple.pyi (and tuple.pyi) to …
tyralla Nov 21, 2023
dcd12ba
Consider TypeType as hashable if (if no metaclass sets __hash__ to None)
tyralla Nov 24, 2023
e25f8e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 24, 2023
ed0f652
Look for __hash__ instead of checking the name of the potential Hashable
tyralla Nov 24, 2023
a46e5d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 24, 2023
721465a
Merge branch 'master' into fix/narrow_type_vs_Type
tyralla Nov 26, 2023
7d1d3f2
Add another `TypeVar`-related test to `testBuiltinTypeType`.
tyralla Nov 26, 2023
7107fb8
Add more test cases to `testBuiltinTypeType`.
tyralla Nov 27, 2023
3858928
Check more thoroughly if the `Protocol` is `Hashable`-like.
tyralla Nov 27, 2023
a09c6e2
Check more thoroughly if the signatures agree.
tyralla Nov 27, 2023
1a2202f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 27, 2023
4330ea0
remove unused local symtab variable
tyralla Dec 11, 2023
d6e4338
Merge remote-tracking branch 'upstream/master' into fix/narrow_type_v…
hauntsaninja Nov 3, 2024
c7dbe6a
fix merge
hauntsaninja Nov 3, 2024
5a3e028
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Find other places where analyze_annotation should eventually be set…
… to true.
  • Loading branch information
tyralla committed Nov 15, 2023
commit fe0cd0a5b6deae9aad25cf8b0bc2e929c1ccfeaa
6 changes: 4 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5375,6 +5375,7 @@ def analyze_type_application_args(self, expr: IndexExpr) -> list[Type] | None:
allow_placeholder=True,
allow_param_spec_literals=has_param_spec,
allow_unpack=allow_unpack,
analyze_annotation=True,
)
if analyzed is None:
return None
Expand All @@ -5399,7 +5400,7 @@ def visit_slice_expr(self, expr: SliceExpr) -> None:

def visit_cast_expr(self, expr: CastExpr) -> None:
expr.expr.accept(self)
analyzed = self.anal_type(expr.type)
analyzed = self.anal_type(expr.type, analyze_annotation=True)
if analyzed is not None:
expr.type = analyzed

Expand All @@ -5421,7 +5422,7 @@ def visit_reveal_expr(self, expr: RevealExpr) -> None:
def visit_type_application(self, expr: TypeApplication) -> None:
expr.expr.accept(self)
for i in range(len(expr.types)):
analyzed = self.anal_type(expr.types[i])
analyzed = self.anal_type(expr.types[i], analyze_annotation=True)
if analyzed is not None:
expr.types[i] = analyzed

Expand Down Expand Up @@ -6579,6 +6580,7 @@ def expr_to_analyzed_type(
allow_unbound_tvars=allow_unbound_tvars,
allow_param_spec_literals=allow_param_spec_literals,
allow_unpack=allow_unpack,
analyze_annotation=isinstance(expr, IndexExpr),
)

def analyze_type_expr(self, expr: Expression) -> None:
Expand Down
1 change: 1 addition & 0 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def analyze_type_alias(
allow_placeholder=allow_placeholder,
prohibit_self_type="type alias target",
allowed_alias_tvars=allowed_alias_tvars,
analyze_annotation=True,
)
analyzer.in_dynamic_func = in_dynamic_func
analyzer.global_scope = global_scope
Expand Down
11 changes: 0 additions & 11 deletions test-data/unit/check-callable.test
Original file line number Diff line number Diff line change
Expand Up @@ -524,17 +524,6 @@ else:

[builtins fixtures/callable.pyi]

[case testBuiltinsTypeAsCallable]
# flags: --python-version 3.7
from __future__ import annotations

reveal_type(type) # N: Revealed type is "def (x: Any) -> builtins.type"
_TYPE = type
reveal_type(_TYPE) # N: Revealed type is "def (x: Any) -> builtins.type"
_TYPE('bar')

[builtins fixtures/callable.pyi]

[case testErrorMessageAboutSelf]
# https://github.com/python/mypy/issues/11309
class Some:
Expand Down
37 changes: 37 additions & 0 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -3235,3 +3235,40 @@ class b:
x = x[1] # E: Cannot resolve name "x" (possible cyclic definition)
y = 1[y] # E: Value of type "int" is not indexable \
# E: Cannot determine type of "y"

[case testBuiltinTypeType]
# flags: --python-version 3.10 --disallow-any-generics

from typing import Any, cast, Union
from typing_extensions import TypeAlias

a: type # E: Missing type parameters for generic type "type"
reveal_type(a) # N: Revealed type is "Type[Any]"

b: Any
c = cast(type, b) # E: Missing type parameters for generic type "type"
reveal_type(c) # N: Revealed type is "Type[Any]"

d: list[type] # E: Missing type parameters for generic type "type"
reveal_type(d) # N: Revealed type is "builtins.list[Type[Any]]"

class E(list[type]): ... # E: Missing type parameters for generic type "type"
reveal_type(E()[0]) # N: Revealed type is "Type[Any]"

class F(tuple[type]): ... # E: Missing type parameters for generic type "type"
reveal_type(F()[0]) # N: Revealed type is "Type[Any]"

class G(tuple[list[Union[type, int]], ...]): ... # E: Missing type parameters for generic type "type"
reveal_type(G()[0]) # N: Revealed type is "builtins.list[Union[Type[Any], builtins.int]]"

h: Union[type, int] # E: Missing type parameters for generic type "type"
reveal_type(h) # N: Revealed type is "Union[Type[Any], builtins.int]"

i: type | int # E: Missing type parameters for generic type "type"
reveal_type(i) # N: Revealed type is "Union[Type[Any], builtins.int]"

j: TypeAlias = type # E: Missing type parameters for generic type "type"
k: j
reveal_type(k) # N: Revealed type is "Type[Any]"

[builtins fixtures/tuple.pyi]
0