Skip to content

Class typevars lose information from isinstance checks #5720

Closed

Description

@msullivan
from typing import Generic, TypeVar

T = TypeVar('T')

class C(Generic[T]):
    def foo(self, x: T) -> None:
        if isinstance(x, int):
            self.bar(x)

    def bar(self, x: T) -> None:
        pass

fails with Argument 1 to "bar" of "C" has incompatible type "int"; expected "T", even though x is obviously a T (that was its annotated type!)

Arguably this is just the Dreaded Intersection Bug (#3603) in a different manifestation?

Activity

  1. added
    bugmypy got something wrong
    false-positivemypy gave an error on correct code
    on Oct 3, 2018
  2. ilevkivskyi commented on Oct 3, 2018

    @ilevkivskyi
    Member

    Yes, and this is probably a hardest aspect of the problem, because I don't see an easy way to work around this using some fake TypeInfos for intersections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions