GeezerVison is showing github.com — fix enum+literal getting coerced to Sequence[str] · python/mypy@1fd8382
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: mypy/join.py +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -150,6 +150,12 @@ def join_instances(self, t: Instance, s: Instance) -> ProperType: |
150 | 150 | t, s, subtype_context=SubtypeContext(ignore_type_params=True) |
151 | 151 | ): |
152 | 152 | result = self.join_instances_via_supertype(t, s) |
| 153 | + elif s.type.bases and is_proper_subtype( |
| 154 | + s, t, subtype_context=SubtypeContext(ignore_type_params=True) |
| 155 | + ): |
| 156 | + result = self.join_instances_via_supertype(s, t) |
| 157 | + elif is_subtype(t, s, subtype_context=SubtypeContext(ignore_type_params=True)): |
| 158 | + result = self.join_instances_via_supertype(t, s) |
153 | 159 | else: |
154 | 160 | # Now t is not a subtype of s, and t != s. Now s could be a subtype |
155 | 161 | # of t; alternatively, we need to find a common supertype. This works |
|
Collapse file
Copy file name to clipboardExpand all lines: test-data/unit/check-literal.test +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3006,3 +3006,20 @@ def check_co(obj: A_co[Literal[1]]) -> None: |
3006 | 3006 | reveal_type(f(obj, '')) # N: Revealed type is "builtins.object" |
3007 | 3007 | reveal_type(g(1, obj)) # N: Revealed type is "builtins.int" |
3008 | 3008 | reveal_type(g('', obj)) # N: Revealed type is "builtins.object" |
| 3009 | + |
| 3010 | +[case testJoinLiteralInstanceAndEnum] |
| 3011 | +from typing import Final, TypeVar |
| 3012 | +from enum import StrEnum |
| 3013 | + |
| 3014 | +T = TypeVar("T") |
| 3015 | +def join(a: T, b: T) -> T: ... |
| 3016 | + |
| 3017 | +class Foo(StrEnum): |
| 3018 | + A = "a" |
| 3019 | + |
| 3020 | +CONST: Final = "const" |
| 3021 | + |
| 3022 | +reveal_type(CONST) # N: Revealed type is "Literal['const']?" |
| 3023 | +reveal_type(join(Foo.A, CONST)) # N: Revealed type is "builtins.str" |
| 3024 | +reveal_type(join(CONST, Foo.A)) # N: Revealed type is "builtins.str" |
| 3025 | +[builtins fixtures/tuple.pyi] |