KEMBAR78
Self-type in class method doesn't work on unions · Issue #7821 · python/mypy · GitHub
Skip to content

Self-type in class method doesn't work on unions #7821

@ilevkivskyi

Description

@ilevkivskyi

This test case:

[case testSelfTypeClassMethodOnUnion]
from typing import Type, Union, TypeVar

T = TypeVar('T')

class A:
    @classmethod
    def meth(cls: Type[T]) -> T: ...
class B(A): ...
class C(A): ...

t: Type[Union[B, C]]
x = t.meth()
reveal_type(x)
[builtins fixtures/classmethod.pyi]

fails with

main:12: error: Need type annotation for 'x'
main:13: note: Revealed type is 'Any'

while the type should clearly be Union[B, C].

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions