-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
No exception for missing attributes with __getattr__ #6251
Comments
I think in this case, the issue isn't that (Normally, mypy will report an error with the import in cases like these, but it looks like your docs tell people to suppress it with the Anyways, I think you can fix this by making your package PEP 561 compatible -- basically, add type hints to your code, or bundle a separate set of type stubs files with your library. Probably the latter might be easiest in your case? You can add type stubs for just the classes that your mypy users are likely to use (and omit a definition for Regarding the getattr thing -- I believe you can make mypy ignore the from typing import no_type_check
class Base:
@no_type_check
def __getattr__(self, name):
pass
class Model(Base):
age: int
m = Model()
reveal_type(m.foo) ...I get the following error messages:
If I remove the annotation, the "Model has no attribute" error stops showing up. (But making just this change to your code would be moot for your users if you don't also make your package PEP 561 compatible.) |
Thanks @Michael0x2a that's very helpful, as it happens I have been discussing adding type hints today anyway. The reason I hadn't worked hard on type hints already was some confusion about different PEPs/python versions and what the best way to add type hints was. Some people seemed to suggest stub files while others said: "just add types to your code", there was also typeshed and "*-stubs" packages. The verdict today seems to be "just add types to your code" so I'll do that. |
Thanks so much for the help, this is now fixed in pydantic. For the record and anyone else trying to do this sort of thing: I had to add |
Mypy did not raise an error on a missing method when base class defined a `__getattr__` function, so make mypy ignore that method by decorating `__getattr__` with `typing.no_type_check`. related issue: python/mypy#6251
Mypy does not raise an error on a `missing method` when base class defined a `__getattr__` function, so make mypy ignore that method by decorating `__getattr__` with `typing.no_type_check`. related issue: python/mypy#6251
Mypy does not raise an error on a `missing method` when base class defines a `__getattr__` function, so make mypy ignore that method by decorating `__getattr__` with `typing.no_type_check`. related issue: python/mypy#6251
Mypy does not raise an error on a `missing method` when base class defines a `__getattr__` function, so make mypy ignore that method by decorating `__getattr__` with `typing.no_type_check`. related issue: python/mypy#6251 PR Closed: Graviti-AI#607
Mypy does not raise an error on a `missing method` when base class defines a `__getattr__` function, so make mypy ignore that method by decorating `__getattr__` with `typing.no_type_check`. related issue: python/mypy#6251 PR Closed: #607
(Apologies if this has already been answered somewhere else, I've looked but couldn't find a duplicate, it's kind of the opposite of #520)
I'm trying to get pydantic to work better with mypy. This is taken from pydantic/pydantic#245.
This code obviously raises an
AttributeError
with python, but mypy doesn't complain since as per #520 the__getattr__
method is defined so mypy can't know ifm.foobar
exists.Is there any way to get mypy to fail in a case like this?
The only thing I can think of is to define
BaseModel
differently based onTYPE_CHECKING
so__getattr__
isn't defined in that case. This that the best/only way to go or is there some magic comment or annotation I can add to get mypy to ignore__getattr__
?The text was updated successfully, but these errors were encountered: