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

Object (abstract) property wrongly seen as TypeVar or generic type in isinstance #1294

Closed
Fatsie opened this issue May 16, 2021 · 2 comments
Closed
Labels
fixed in next version (main) A fix has been implemented and will appear in an upcoming version

Comments

@Fatsie
Copy link

Fatsie commented May 16, 2021

Environment data

  • Language Server version: 2021.5.1 (pyright df8b6b3c)
  • OS and version: Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.9 64-bit (conda)

Code Snippet

from abc import abstractmethod
from typing import Any, Union, Generic, TypeVar, Type, Tuple

T = TypeVar("T")

class A(Generic[T]):
    @property
    @abstractmethod
    def _elem_type_(self) -> Union[Type[T], Tuple[Type[T], ...]]:
        raise NotImplementedError
    
    def check_type(self, var: Any) -> bool:
        return isinstance(var, self._elem_type_)
    
    def execute(self, var: Union[T, Tuple[T]]) -> None:
        if isinstance(var, self._elem_type_):
            print("single")
        else:
            print("iterable")

Expected behaviour

Not give a Pylance error

Actual behaviour

The use of isinstance() in check_type() method is OK; the use of isinstance() in execute() gives the following Pylance typing error:

{
	"resource": "/home/verhaegs/eda/test/test_generic.py",
	"owner": "_generated_diagnostic_collection_name_#2",
	"code": {
		"value": "reportGeneralTypeIssues",
		"target": {
			"$mid": 1,
			"external": "https://github.com/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md#diagnostic-severity-rules",
			"path": "/microsoft/pylance-release/blob/main/DIAGNOSTIC_SEVERITY_RULES.md",
			"scheme": "https",
			"authority": "github.com",
			"fragment": "diagnostic-severity-rules"
		}
	},
	"severity": 8,
	"message": "Second argument to \"isinstance\" must be a class or tuple of classes\n  TypeVar or generic type with type arguments not allowed",
	"source": "Pylance",
	"startLineNumber": 16,
	"startColumn": 28,
	"endLineNumber": 16,
	"endColumn": 44
}
@erictraut
Copy link
Contributor

Thanks for the bug report. This will be addressed in the next release.

@erictraut erictraut added the fixed in next version (main) A fix has been implemented and will appear in an upcoming version label May 16, 2021
@github-actions github-actions bot removed the triage label May 16, 2021
@jakebailey
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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