Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic object attribute with contraints wrongly seen as TypeVar or Generic type in isinstance #1730

Closed
hojo0590 opened this issue Aug 26, 2021 · 2 comments
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@hojo0590
Copy link

Environment data

  • Language Server version: 2021.8.3 (pyright fa4194fd)
  • OS and version: Win10 19043.1165
  • Python version (& distribution if applicable, e.g. Anaconda): 3.8.11 (Anaconda)

Code Snippet

from typing import Any, Generic, TypeVar

T = TypeVar("T", int, str)


class B(Generic[T]):
    att: T

    def check_type(self, var: Any) -> bool:
        return isinstance(var, type(self.att))

Expected behaviour

I'd expect pylance to not throw an Second argument to "isinstance" must be a class or tuple of classes TypeVar or generic type with type arguments not allowed error on the second argument passed to isisntance().

Actual behaviour

Error is thrown.

Additional information

Possibly related/in addition to #1294 and
using a TypeVar without constraints works well.

from typing import Any, Generic, TypeVar

T = TypeVar("T")


class B(Generic[T]):
    att: T

    def check_type(self, var: Any) -> bool:
        return isinstance(var, type(self.att))

(removing the constraints TypeVar("T", int, str) -> TypeVar("T") does not lead to an error)

@erictraut
Copy link
Contributor

Thanks for the bug report. I agree this is a bug. It occurs only with a constrained TypeVar. The fix will be in the next release.

@jakebailey jakebailey added bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version labels Aug 26, 2021
@github-actions github-actions bot removed the triage label Aug 26, 2021
@jakebailey
Copy link
Member

This issue has been fixed in version 2021.7.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202190-1-september-2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in next version (main) A fix has been implemented and will appear in an upcoming version
Projects
None yet
Development

No branches or pull requests

3 participants