Skip to content

Commit 1fd8382

Browse files
fix enum+literal getting coerced to Sequence[str]
1 parent 81b63cf commit 1fd8382

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

mypy/join.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ def join_instances(self, t: Instance, s: Instance) -> ProperType:
150150
t, s, subtype_context=SubtypeContext(ignore_type_params=True)
151151
):
152152
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)
153159
else:
154160
# Now t is not a subtype of s, and t != s. Now s could be a subtype
155161
# of t; alternatively, we need to find a common supertype. This works

test-data/unit/check-literal.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,3 +3006,20 @@ def check_co(obj: A_co[Literal[1]]) -> None:
30063006
reveal_type(f(obj, '')) # N: Revealed type is "builtins.object"
30073007
reveal_type(g(1, obj)) # N: Revealed type is "builtins.int"
30083008
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]

0 commit comments

Comments
 (0)