You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unintuitive behaviour when only subpackage provides py.typed #16149
from numba import typed causes mypy to generate spurious attr-defined errors for everything else imported from numba.
To Reproduce
fromnumbaimportnjit, typed# error: Module has no attribute "njit" [attr-defined]
Expected Behavior
scratch.py:1: error: Skipping analyzing "numba": module is installed, but missing library stubs or py.typed marker [import]
scratch.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Actual Behavior
scratch.py:1: error: Module "numba" has no attribute "njit" [attr-defined]
Your Environment
Mypy version used: 1.5.1
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
This is because the numba.typed subpackage provides a py.typed file. As per PEP 561, I think this makes it look like a part of a typed namespace package that only provides that subpackage (see https://peps.python.org/pep-0561/#stub-only-packages). I think this is behaving as per the spec?
I don't know what LukeSavefrogs is about, I cannot reproduce nor have you provided enough information to truly reproduce.
This is because the numba.typed subpackage provides a py.typed file. As per PEP 561, I think this makes it look like a part of a typed namespace package that only provides that subpackage (see https://peps.python.org/pep-0561/#stub-only-packages). I think this is behaving as per the spec?
Thanks. I hadn't noticed the py.typed file in the numba.typed sub-package.
I don't think numba.typed qualifies as a namespace package in this case. My understanding per PEP 561 is that a sub-package can include a py.typed file to indicate that it and its sub-packages support typing. But in this case, it seems that mypy is seeing the py.typed file in the numba.typed sub-package and applying it to the entire numba package. To demonstrate this, note that omitting typed from the import results in the expected behavior:
fromnumbaimportnjit
Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing. This marker applies recursively: if a top-level package includes it, all its sub-packages MUST support type checking as well.
In my case, I cannot simply disable error by code for whole module (readline), though disabling globally in mypy works
[mypy]strict = True
; # beware of enabling option, it can lead to false positives; disable_error_code = attr-defined; this doesn't work xD[mypy-readline]disable_error_code = attr-defined
[mypy-readline.*]disable_error_code = attr-defined
I'm not sure if this bug has subsequently been fixed. Assuming it hasn't, another workaround that might help is --follow-untyped-imports. This will try to analyze all the files. They probably won't get analyzed very well, but this is probably a bit more type safe than just skipping all analysis, if it works in one's particular case (it will probably also skip a lot of analysis, effectively, but it will do what it can).
This is still an issue (at least through mypy==1.18.2). For the specific issue I'm dealing with*, using follow-untyped-imports does indeed ~bypass the problem.
But then it explodes with a ton of other errors as you suggest might happen.
Activity
LukeSavefrogs commentedon Sep 20, 2023
The same is happening with
pydantic.Example
Take the following snippet as example:
This code will produce the following
mypyerrors:Name "pydantic.BaseModel" is not defined [name-defined].Module has no attribute "Field" [attr-defined].hauntsaninja commentedon Sep 21, 2023
This is because the numba.typed subpackage provides a
py.typedfile. As per PEP 561, I think this makes it look like a part of a typed namespace package that only provides that subpackage (see https://peps.python.org/pep-0561/#stub-only-packages). I think this is behaving as per the spec?I don't know what LukeSavefrogs is about, I cannot reproduce nor have you provided enough information to truly reproduce.
LukeSavefrogs commentedon Sep 21, 2023
Sorry if I was not clear, when I come back home I'll try and see if the issue is related to what you said. Otherwise I'll open a new issue. 😄
Ranfir commentedon Sep 21, 2023
Thanks. I hadn't noticed the
py.typedfile in thenumba.typedsub-package.I don't think
numba.typedqualifies as a namespace package in this case. My understanding per PEP 561 is that a sub-package can include apy.typedfile to indicate that it and its sub-packages support typing. But in this case, it seems that mypy is seeing thepy.typedfile in thenumba.typedsub-package and applying it to the entirenumbapackage. To demonstrate this, note that omittingtypedfrom the import results in the expected behavior:LukeSavefrogs commentedon Sep 25, 2023
As an addition, I can confirm that
pydantictoo has apy.typedfilec-gohlke commentedon Mar 21, 2024
is there any solution/workaround to this?
I can add
to pyproject.toml, but I would rather this happens at a module level
does not work because the error happens in my modules at import time
sumkincpp commentedon Jun 30, 2024
In my case, I cannot simply disable error by code for whole module (readline), though disabling globally in mypy works
danielhundhausen commentedon Oct 9, 2024
Is there any update on this or recommended way of dealing with it?
fix mypy 'attr-defined' error with matplotlib.cycler by ignoring it. …
hauntsaninja commentedon Oct 10, 2024
I don't know about recommended, but here's one possible workaround:
It will make mypy simply skip analysis of numba and anything originating from it will be Any.
[-]Module has no attribute[/-][+]Unintuitive behaviour when only subpackage provides py.typed[/+]brandonchinn178 commentedon Apr 4, 2025
+1 types-protobuf has the same issue, where
errors with "Library google not found"
python/typeshed#10986 (comment)
nipunn1313/mypy-protobuf#681
wyattscarpenter commentedon Nov 6, 2025
I'm not sure if this bug has subsequently been fixed. Assuming it hasn't, another workaround that might help is
--follow-untyped-imports. This will try to analyze all the files. They probably won't get analyzed very well, but this is probably a bit more type safe than just skipping all analysis, if it works in one's particular case (it will probably also skip a lot of analysis, effectively, but it will do what it can).BryceBeagle commentedon Nov 6, 2025
This is still an issue (at least through
mypy==1.18.2). For the specific issue I'm dealing with*, usingfollow-untyped-importsdoes indeed ~bypass the problem.But then it explodes with a ton of other errors as you suggest might happen.
*
mypy-protobufgenerated code +types-protobufstubs. See: nipunn1313/mypy-protobuf#681BryceBeagle commentedon Nov 6, 2025
Actually, looking at this again:
I had initially tried using
but doing just
seems to be just the right about of granularity. The imports I care about are no longer
Any, but no other random errors in subpackages get surfaced.Synthesize __getattr__ on untyped parents of typed subpackages
Synthesize __getattr__ on untyped parents of typed subpackages
Synthesize __getattr__ on untyped parents of typed subpackages