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

error: Missing type parameters for generic type "array" #13942

Closed
sg495 opened this issue Oct 25, 2022 · 1 comment
Closed

error: Missing type parameters for generic type "array" #13942

sg495 opened this issue Oct 25, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@sg495
Copy link

sg495 commented Oct 25, 2022

Bug Report

Mypy strict mode complains if a type parameter is omitted when using the built-in array type in an annotation, while Python raises TypeError if a type parameter is included. Mypy only complains in strict mode.

To Reproduce

from array import array
x: array = array("L")      # Mypy error: Missing type parameters for generic type "array"
y: array[int] = array("L") # TypeError: 'type' object is not subscriptable

Expected Behavior

Mypy strict mode should not complain about a missing generic type parameter (since Python does not allow it).

Actual Behavior

array-mypy.py:2: error: Missing type parameters for generic type "array"

Possible Workaround

from array import array
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    IntArray = array[int]
else:
    IntArray = array

x: IntArray = array("L")

Your Environment

  • Mypy version used: mypy 0.982 (compiled: yes)
  • Mypy command-line flags: mypy --strict
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.13
@sg495 sg495 added the bug mypy got something wrong label Oct 25, 2022
@JelleZijlstra
Copy link
Member

See https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime for general guidance.

However, in this case we should probably make array support __class_getitem__ at runtime. I can't find a reason why we didn't do that before. I'll submit an issue to CPython to make that change, but it won't be released until 3.12.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2022
robin-nitrokey added a commit to Nitrokey/pynitrokey that referenced this issue Jan 10, 2025
The array class is not subscriptable in Python 3.11 and older, causing
a TypeError at runtime.  This patch applies the workaround suggested in
python/mypy#13942.
robin-nitrokey added a commit to Nitrokey/pynitrokey that referenced this issue Jan 10, 2025
The array class is not subscriptable in Python 3.11 and older, causing
a TypeError at runtime.  This patch applies the workaround suggested in
python/mypy#13942.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants