From 8923d90443e733941ee72f4371b41b4c11e1b00a Mon Sep 17 00:00:00 2001 From: STerliakov Date: Mon, 7 Jul 2025 23:24:41 +0200 Subject: [PATCH] Return from is_subtype early --- misc/perf_compare.py | 2 ++ mypy/subtypes.py | 4 ++++ test-data/unit/check-generics.test | 4 ++-- test-data/unit/check-parameter-specification.test | 8 ++++---- 4 files changed, 12 insertions(+), 6 deletions(-) mode change 100644 => 100755 misc/perf_compare.py diff --git a/misc/perf_compare.py b/misc/perf_compare.py old mode 100644 new mode 100755 index 39dd22b31339..7d22f43d1c45 --- a/misc/perf_compare.py +++ b/misc/perf_compare.py @@ -1,3 +1,5 @@ +#! /usr/bin/env python + """Compare performance of mypyc-compiled mypy between one or more commits/branches. Simple usage: diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 428e6dec6749..1aa8543505ec 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -145,6 +145,8 @@ def is_subtype( between the type arguments (e.g., A and B), taking the variance of the type var into account. """ + if left == right: + return True if subtype_context is None: subtype_context = SubtypeContext( ignore_type_params=ignore_type_params, @@ -206,6 +208,8 @@ def is_proper_subtype( (this is useful for runtime isinstance() checks). If keep_erased_types is True, do not consider ErasedType a subtype of all types (used by type inference against unions). """ + if left == right: + return True if subtype_context is None: subtype_context = SubtypeContext( ignore_promotions=ignore_promotions, diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 0be9d918c69f..78680684f69b 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -2929,8 +2929,8 @@ def mix(fs: List[Callable[[S], T]]) -> Callable[[S], List[T]]: def id(__x: U) -> U: ... fs = [id, id, id] -reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`11) -> builtins.list[S`11]" -reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`13) -> builtins.list[S`13]" +reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`7) -> builtins.list[S`7]" +reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`9) -> builtins.list[S`9]" [builtins fixtures/list.pyi] [case testInferenceAgainstGenericCurry] diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index e53c45b5b512..0835ba7ac57d 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -921,8 +921,8 @@ class A: def func(self, action: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R: ... -reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`6, *_P.args, **_P.kwargs) -> _R`6" -reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`10, *_P.args, **_P.kwargs) -> _R`10" +reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`4, *_P.args, **_P.kwargs) -> _R`4" +reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`8, *_P.args, **_P.kwargs) -> _R`8" def f(x: int) -> int: ... @@ -953,8 +953,8 @@ class A: def func(self, action: Job[_P, None]) -> Job[_P, None]: ... -reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`4, None]) -> __main__.Job[_P`4, None]" -reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`6, None]) -> __main__.Job[_P`6, None]" +reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`3, None]) -> __main__.Job[_P`3, None]" +reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`5, None]) -> __main__.Job[_P`5, None]" reveal_type(A().func(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]" def f(x: int, y: int) -> None: ...