From 7c31a94767fb96cd0db351372f3b6096ac49771a Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 11 Dec 2024 00:19:58 +0300 Subject: [PATCH 1/5] Do not allow `type[]` to contain `Literal` types --- mypy/typeanal.py | 6 +++--- mypy/types_utils.py | 5 +++-- test-data/unit/check-literal.test | 9 +++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mypy/typeanal.py b/mypy/typeanal.py index 2f85e83bb3c3..22da40a1548a 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -652,14 +652,14 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ # To prevent assignment of 'builtins.type' inferred as 'builtins.object' # See https://github.com/python/mypy/issues/9476 for more information return None + type_str = "Type[...]" if fullname == "typing.Type" else "type[...]" if len(t.args) != 1: - type_str = "Type[...]" if fullname == "typing.Type" else "type[...]" self.fail( - type_str + " must have exactly one type argument", t, code=codes.VALID_TYPE + f"{type_str} must have exactly one type argument", t, code=codes.VALID_TYPE ) item = self.anal_type(t.args[0]) if is_bad_type_type_item(item): - self.fail("Type[...] can't contain another Type[...]", t, code=codes.VALID_TYPE) + self.fail(f'{type_str} can\'t contain "{item}"', t, code=codes.VALID_TYPE) item = AnyType(TypeOfAny.from_error) return TypeType.make_normalized(item, line=t.line, column=t.column) elif fullname == "typing.ClassVar": diff --git a/mypy/types_utils.py b/mypy/types_utils.py index 1cd56eae5835..1f3483193a75 100644 --- a/mypy/types_utils.py +++ b/mypy/types_utils.py @@ -26,6 +26,7 @@ TypeVarType, UnionType, UnpackType, + LiteralType, flatten_nested_unions, get_proper_type, get_proper_types, @@ -83,11 +84,11 @@ def is_bad_type_type_item(item: Type) -> bool: TypeType item is normalized (i.e. always a proper type). """ item = get_proper_type(item) - if isinstance(item, TypeType): + if isinstance(item, (TypeType, LiteralType)): return True if isinstance(item, UnionType): return any( - isinstance(get_proper_type(i), TypeType) for i in flatten_nested_unions(item.items) + is_bad_type_type_item(typ) for typ in flatten_nested_unions(item.items) ) return False diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index 2f94b5df0f83..cc9abf91da5e 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -2984,3 +2984,12 @@ class C(Base): reveal_type(sep) # N: Revealed type is "Union[Literal['a'], Literal['b']]" return super().feed_data(sep) [builtins fixtures/tuple.pyi] + +[case testLiteralInsideAType] +from typing_extensions import Literal +from typing import Type, Union + +x: Type[Literal[1]] # E: Type[...] can't contain "Literal[1]" +y: Type[Union[Literal[1], Literal[2]]] # E: Type[...] can't contain "Union[Literal[1], Literal[2]]" +z: Type[Literal[1, 2]] # E: Type[...] can't contain "Union[Literal[1], Literal[2]]" +[builtins fixtures/tuple.pyi] From ef9a659b86d9d9afb08fcf9a13014819cbaafe96 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:23:29 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/types_utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mypy/types_utils.py b/mypy/types_utils.py index 1f3483193a75..f845186f5d78 100644 --- a/mypy/types_utils.py +++ b/mypy/types_utils.py @@ -15,6 +15,7 @@ AnyType, CallableType, Instance, + LiteralType, NoneType, Overloaded, ParamSpecType, @@ -26,7 +27,6 @@ TypeVarType, UnionType, UnpackType, - LiteralType, flatten_nested_unions, get_proper_type, get_proper_types, @@ -87,9 +87,7 @@ def is_bad_type_type_item(item: Type) -> bool: if isinstance(item, (TypeType, LiteralType)): return True if isinstance(item, UnionType): - return any( - is_bad_type_type_item(typ) for typ in flatten_nested_unions(item.items) - ) + return any(is_bad_type_type_item(typ) for typ in flatten_nested_unions(item.items)) return False From 054dd1f76df76e21035ddd08d7901b99157a5192 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 11 Dec 2024 00:45:32 +0300 Subject: [PATCH 3/5] Fix CI --- mypy/types_utils.py | 2 ++ test-data/unit/check-recursive-types.test | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mypy/types_utils.py b/mypy/types_utils.py index f845186f5d78..c54426f1dcd2 100644 --- a/mypy/types_utils.py +++ b/mypy/types_utils.py @@ -82,6 +82,8 @@ def is_bad_type_type_item(item: Type) -> bool: Such types are explicitly prohibited by PEP 484. Also, they cause problems with recursive types like T = Type[T], because internal representation of TypeType item is normalized (i.e. always a proper type). + + Also forbids `Type[Literal[...]]`, because typing spec does not allow it. """ item = get_proper_type(item) if isinstance(item, (TypeType, LiteralType)): diff --git a/test-data/unit/check-recursive-types.test b/test-data/unit/check-recursive-types.test index 4d7af98204fb..3856fba646ae 100644 --- a/test-data/unit/check-recursive-types.test +++ b/test-data/unit/check-recursive-types.test @@ -409,8 +409,10 @@ def local() -> None: x: L reveal_type(x) # N: Revealed type is "builtins.list[Union[builtins.int, Any]]" -S = Type[S] # E: Type[...] can't contain another Type[...] -U = Type[Union[int, U]] # E: Type[...] can't contain another Type[...] +S = Type[S] # E: Type[...] can't contain "type[]" \ + # E: Type[...] can't contain "type[Any]" +U = Type[Union[int, U]] # E: Type[...] can't contain "Union[builtins.int, Union[type[builtins.int], type[]]]" \ + # E: Type[...] can't contain "Union[builtins.int, type[Any]]" x: U reveal_type(x) # N: Revealed type is "Type[Any]" From c0ed4e3dc3f062d4595b5965470010459644e255 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 11 Dec 2024 01:57:36 +0300 Subject: [PATCH 4/5] Fix CI --- mypy/typeanal.py | 7 ++++--- mypy/types_utils.py | 22 +++++++++++++++++----- test-data/unit/check-literal.test | 6 +++--- test-data/unit/check-recursive-types.test | 7 +++---- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mypy/typeanal.py b/mypy/typeanal.py index 22da40a1548a..bc340c194cdc 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -110,7 +110,7 @@ get_proper_type, has_type_vars, ) -from mypy.types_utils import is_bad_type_type_item +from mypy.types_utils import get_bad_type_type_item from mypy.typevars import fill_typevars T = TypeVar("T") @@ -658,8 +658,9 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ f"{type_str} must have exactly one type argument", t, code=codes.VALID_TYPE ) item = self.anal_type(t.args[0]) - if is_bad_type_type_item(item): - self.fail(f'{type_str} can\'t contain "{item}"', t, code=codes.VALID_TYPE) + bad_item_name = get_bad_type_type_item(item) + if bad_item_name: + self.fail(f'{type_str} can\'t contain "{bad_item_name}"', t, code=codes.VALID_TYPE) item = AnyType(TypeOfAny.from_error) return TypeType.make_normalized(item, line=t.line, column=t.column) elif fullname == "typing.ClassVar": diff --git a/mypy/types_utils.py b/mypy/types_utils.py index c54426f1dcd2..5a6d970da026 100644 --- a/mypy/types_utils.py +++ b/mypy/types_utils.py @@ -76,7 +76,7 @@ def is_invalid_recursive_alias(seen_nodes: set[TypeAlias], target: Type) -> bool return False -def is_bad_type_type_item(item: Type) -> bool: +def get_bad_type_type_item(item: Type) -> str | None: """Prohibit types like Type[Type[...]]. Such types are explicitly prohibited by PEP 484. Also, they cause problems @@ -85,12 +85,24 @@ def is_bad_type_type_item(item: Type) -> bool: Also forbids `Type[Literal[...]]`, because typing spec does not allow it. """ + # TODO: what else cannot be present in `type[...]`? item = get_proper_type(item) - if isinstance(item, (TypeType, LiteralType)): - return True + if isinstance(item, TypeType): + return 'Type[...]' + if isinstance(item, LiteralType): + return 'Literal[...]' if isinstance(item, UnionType): - return any(is_bad_type_type_item(typ) for typ in flatten_nested_unions(item.items)) - return False + items = [ + bad_item + for typ in flatten_nested_unions(item.items) + if (bad_item := get_bad_type_type_item(typ)) is not None + ] + if not items: + return None + if len(items) == 1: + return items[0] + return f"Union[{', '.join(items)}]" + return None def is_union_with_any(tp: Type) -> bool: diff --git a/test-data/unit/check-literal.test b/test-data/unit/check-literal.test index cc9abf91da5e..b2d3024d3b44 100644 --- a/test-data/unit/check-literal.test +++ b/test-data/unit/check-literal.test @@ -2989,7 +2989,7 @@ class C(Base): from typing_extensions import Literal from typing import Type, Union -x: Type[Literal[1]] # E: Type[...] can't contain "Literal[1]" -y: Type[Union[Literal[1], Literal[2]]] # E: Type[...] can't contain "Union[Literal[1], Literal[2]]" -z: Type[Literal[1, 2]] # E: Type[...] can't contain "Union[Literal[1], Literal[2]]" +x: Type[Literal[1]] # E: Type[...] can't contain "Literal[...]" +y: Type[Union[Literal[1], Literal[2]]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]" +z: Type[Literal[1, 2]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]" [builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-recursive-types.test b/test-data/unit/check-recursive-types.test index 3856fba646ae..a00a31863771 100644 --- a/test-data/unit/check-recursive-types.test +++ b/test-data/unit/check-recursive-types.test @@ -409,10 +409,9 @@ def local() -> None: x: L reveal_type(x) # N: Revealed type is "builtins.list[Union[builtins.int, Any]]" -S = Type[S] # E: Type[...] can't contain "type[]" \ - # E: Type[...] can't contain "type[Any]" -U = Type[Union[int, U]] # E: Type[...] can't contain "Union[builtins.int, Union[type[builtins.int], type[]]]" \ - # E: Type[...] can't contain "Union[builtins.int, type[Any]]" +S = Type[S] # E: Type[...] can't contain "Type[...]" +U = Type[Union[int, U]] # E: Type[...] can't contain "Union[Type[...], Type[...]]" \ + # E: Type[...] can't contain "Type[...]" x: U reveal_type(x) # N: Revealed type is "Type[Any]" From a8cee9277f727f9d813576e74c4b4482b9dafbc0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:58:04 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/types_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/types_utils.py b/mypy/types_utils.py index 5a6d970da026..aaa7d7fba37a 100644 --- a/mypy/types_utils.py +++ b/mypy/types_utils.py @@ -88,9 +88,9 @@ def get_bad_type_type_item(item: Type) -> str | None: # TODO: what else cannot be present in `type[...]`? item = get_proper_type(item) if isinstance(item, TypeType): - return 'Type[...]' + return "Type[...]" if isinstance(item, LiteralType): - return 'Literal[...]' + return "Literal[...]" if isinstance(item, UnionType): items = [ bad_item