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

ctypes/__init__.pyi uses TypeVar within ClassVar #6333

Closed
erictraut opened this issue Nov 18, 2021 · 4 comments · Fixed by #6348
Closed

ctypes/__init__.pyi uses TypeVar within ClassVar #6333

erictraut opened this issue Nov 18, 2021 · 4 comments · Fixed by #6348

Comments

@erictraut
Copy link
Contributor

I just implemented a check in pyright to report an error if a TypeVar is used within a ClassVar declaration. PEP 526 specifically says:

Note that a ClassVar parameter cannot include any type variables, regardless of the level of nesting: ClassVar[T] and ClassVar[List[Set[T]]] are both invalid if T is a type variable.

A similar feature request was recently filed in the mypy issue tracker.

Here's the issue in the pyright issue tracker: microsoft/pyright#2567.

This check is highlighting a problem in the pointer class defined within the ctypes/__init__.pyi stub.

class pointer(Generic[_CT], _PointerLike, _CData):
    _type_: ClassVar[Type[_CT]]

I don't know enough about ctypes to suggest a proper fix.

@JelleZijlstra
Copy link
Member

At runtime ctypes.pointer is a function that creates (!) a type when you call it, and returns an instance of that type. _type_ is indeed a ClassVar on that type, but probably we should just model it as a regular attribute.

Reference: https://github.com/python/cpython/blob/main/Modules/_ctypes/callproc.c#L1947. The POINTER function right above it is the one that creates the type.

@erictraut
Copy link
Contributor Author

The Array class within the same file also exhibits this problem.

class Array(Generic[_CT], _CData):
    _type_: ClassVar[Type[_CT]]
    ...
``

@sobolevn
Copy link
Member

I can make a PR to mypy to forbid this pattern (python/mypy#11538). Looks like we would need to fix typeshed first.

@sobolevn
Copy link
Member

Mypy PR: python/mypy#11585

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants