-
Notifications
You must be signed in to change notification settings - Fork 114
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 when type checking class inheriting from typing.NamedTuple via pytest #175
Comments
The only use for If the documentation made it seem like typed attribute checking is supported, then I must apologize and ask that you point out where this was. There is an issue (#161) that tracks this feature, should it get implemented in the future. All this said, it should not give you the error it does, so that at least is a bug. |
Sorry, I did not mean to imply that I expect the attribute checking to work properly. I use I think I might be misunderstanding what is not supported. In the example above, the types of the fields seem to get checked. When testing the following with from typing import NamedTuple
import typeguard
@typeguard.typechecked
class C(NamedTuple):
x: float
def test_C():
C("a") I get an error flagging the wrong use of a string where a float is expected:
|
Hm, now I'm confused. I genuinely did not expect this would work. Without the pytest plugin, it certainly doesn't work (thus making the explicit decorator unnecessary). I'll try to fix this ASAP. |
Thank you very much for looking into this! |
Type checking for
from typing import NamedTuple
class C(NamedTuple):
x: float
from typeguard.importhook import install_import_hook
install_import_hook("module")
import module
module.C("a") execute: $ python example.py
[...]
TypeError: type of argument "x" must be either float or int; got str instead I understand it may be a coincidence that this works, and should not be relied upon if it is not supported officially. |
typeguard > 2.11.0 and 2.11.1 have problems with checking types derived from NamedTuple. Upstream issue: agronholm/typeguard#175
fix: avoid using specific versions of typeguardtypeguard > 2.11.0 and 2.11.1 have problems with checking types derived from NamedTuple. Upstream issue: agronholm/typeguard#175 Caused-by: agronholm/typeguard#175 Acked-by: Giel van Schijndel <[email protected]> Acked-by: Kevin de Jong (EHV) <[email protected]> Merged-by: Hopic 1.32.1.dev15+ge9cf4a5
typeguard >= 2.11 has problems with checking types derived from [NamedTuple]. [NamedTuple]: https://docs.python.org/3/library/typing.html#typing.NamedTuple Caused-by: agronholm/typeguard#175 Acked-by: Giel van Schijndel <[email protected]> Acked-by: Joost Muller <[email protected]> Acked-by: Maikel van den Hurk <[email protected]> Acked-by: Rene Kempen <[email protected]> Merged-by: Hopic 1.38.0rc1
This has been resolved in master. |
Describe the bug
Hi, thanks a lot for this extremely useful package! I see errors with classes inheriting from
typing.NamedTuple
withtypeguard
versions2.11.0
/2.11.1
and Python 3.6 / 3.7 when running viapytest
. Python 3.8 and 3.9 do not show the same behavior. I am not sure whether this issue is unique toNamedTuple
, but have only seen it there so far.To Reproduce
Create a new environment with Python 3.6 or 3.7 (I am using docker,
docker run -it --rm python:3.7-slim bash
). Installpytest
andtypeguard
:Now create a file
example.py
containing a type-checked class inheriting fromtyping.NamedTuple
:The
test_C
function is not strictly needed to reproduce the error, but added such that the next step would do something more useful if the error were not present.Running
pytest example.py
results in the following:When downgrading to
typeguard
version2.10.0
, the test runs fine:Expected behavior
I expect
pytest
to run without errors caused bytypeguard
, which is the case for version2.10.0
and for Python versions 3.8 and 3.9.Additional context
none
The text was updated successfully, but these errors were encountered: