KEMBAR78
Cannot determine type error with TypeVar with value constraint · Issue #2697 · python/mypy · GitHub
Skip to content

Cannot determine type error with TypeVar with value constraint #2697

@mohabusama

Description

@mohabusama

Assuming the following code snippet:

from typing import Callable, TypeVar, Generic

T = TypeVar('T', str, int)
Printer = Callable[[T], None]

class Klass(Generic[T]):
    def __init__(self, msg: str, p: Printer[T]) -> None:
        self.msg = msg
        self.p = p

    def echo(self) -> None:
        print(self.msg)

mypy returns error for print(self.msg): error: Cannot determine type of 'msg'

reveal_type fails to determine type of self.msg:
reveal_type(self.msg) results in

error: Cannot determine type of 'msg'
Revealed type is 'Any'

If we changed type variable to unconstrained, T = TypeVar('T'), everything works as expected.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions