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
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
Typeguard version
3.0.1
Python version
3.10
What happened?
Thanks for updating Typeguard, though unfortunately, version 3.0.1 causes an error in my code. I've reduced the error down to the example below, which is hopefully self-explanatory!
C:\Users\Hamish\Git\msm\.venv310\Scripts\python.exe C:\Users\Hamish\Git\msm\msm\typeguardtest.py
Traceback (most recent call last):
File "C:\Users\Hamish\Git\msm\msm\typeguardtest.py", line 11, in <module>
print(a.fub)
File "C:\Users\Hamish\Git\msm\msm\typeguardtest.py", line 7, in fub
def fub(self) -> int:
File "C:\Users\Hamish\Git\msm\.venv310\lib\site-packages\typeguard\_memo.py", line 70, in __init__
self.type_hints = _type_hints_map[func]
File "C:\Users\Hamish\AppData\Local\Programs\Python\Python310\lib\weakref.py", line 416, in __getitem__
return self.data[ref(key)]
TypeError: cannot create weak reference to 'property' object
Process finished with exit code 1
I'm expecting this code to run without any error (and print 5).
Please let me know if you'd like any more information, and thank you for maintaining this package!
It's too bad I didn't have a test covering this case. The changes I'm working on now are really the only way forward. The instrumentation needs to copy the annotations into the function body itself.
The root cause for this is the same as in #315: typeguard's inability to obtain the original function instance to retrieve the correct type hints. I have a fix in the works that bypasses the issue by duplicating the type annotations in the function body.
typeguard throws a spurious error on self._args = args because it mistakenly calls the getter function for _args rather than the setter. This worked fine under the old version so I think it may be related.
The above produces a stack trace like that shown below, but it's just a fancy way of saying that the wrapper is trying to invoke the getter function instead of the setter.
That was fixed in 835fb65 but not part of a release yet. I discovered two other problem with local variable assignments, where iterable unpacking (x, *y = ...) gave incorrect results. I plan to make another patch release once this has been handled.
Sorry, but I'm not sure I can say. But when I had the OPs issue I tried downgrading to typeguard<3.0.0 but then got the error re CallMemo. I was then very confused since after grepping for CallMemo I couldn't find any source code importing it. However after clearing any .pyc files the issue went away.
FYI: 4.0.0rc1 is out. I will likely update the .pyc suffix to -typeguard400 or similar once the final release is out. Until then, continue to clear the .pyc files when you update.
Activity
HJDCampbell commentedon Mar 16, 2023
Also:
intreturn type (so thatfub()has no return type) means the code runs with no error.fub()[-]Property with int return causes error[/-][+]Property with int return type causes error[/+]agronholm commentedon Mar 16, 2023
Yeah, if you remove the annotation, the method doesn't get instrumented since there would be nothing to check for.
agronholm commentedon Mar 16, 2023
It's too bad I didn't have a test covering this case. The changes I'm working on now are really the only way forward. The instrumentation needs to copy the annotations into the function body itself.
agronholm commentedon Mar 18, 2023
The root cause for this is the same as in #315: typeguard's inability to obtain the original function instance to retrieve the correct type hints. I have a fix in the works that bypasses the issue by duplicating the type annotations in the function body.
agronholm commentedon Mar 19, 2023
Would you mind trying the
4.0-devbranch? Your snippet at least works properly with that.corwinjoy commentedon Mar 21, 2023
I actually ran into a similar problem with setters. I have a class that defines an args property like:
and then in the class init method I have
typeguard throws a spurious error on
self._args = argsbecause it mistakenly calls the getter function for _args rather than the setter. This worked fine under the old version so I think it may be related.corwinjoy commentedon Mar 21, 2023
The above produces a stack trace like that shown below, but it's just a fancy way of saying that the wrapper is trying to invoke the getter function instead of the setter.
agronholm commentedon Mar 22, 2023
That was fixed in 835fb65 but not part of a release yet. I discovered two other problem with local variable assignments, where iterable unpacking (
x, *y = ...)gave incorrect results. I plan to make another patch release once this has been handled.HJDCampbell commentedon Mar 22, 2023
That branch works perfectly with my code, it even picked up a small bug! Thank you :)
agronholm commentedon Mar 30, 2023
Yes, this was already reported by the original poster, and is already fixed in the
4.0-devbranch. Do you have something to add?agronholm commentedon Mar 30, 2023
What's importing
CallMemo? There is no mention of that in the entire typeguard codebase in4.0-dev.agronholm commentedon Mar 30, 2023
You're using the wrong branch or revision. CallMemo was entirely removed in a66d170.
AckslD commentedon Mar 30, 2023
Try removing any
.pyc, I had a similar issueagronholm commentedon Mar 30, 2023
But if leftover .pyc files are the issue, where is the source code that shows
CallMemocoming from?AckslD commentedon Mar 30, 2023
Sorry, but I'm not sure I can say. But when I had the OPs issue I tried downgrading to
typeguard<3.0.0but then got the error reCallMemo. I was then very confused since after grepping forCallMemoI couldn't find any source code importing it. However after clearing any.pycfiles the issue went away.agronholm commentedon Mar 30, 2023
At any rate, when upgrading Typeguard, clearing any
-typeguard.pycfiles is a good idea.agronholm commentedon Apr 1, 2023
FYI: 4.0.0rc1 is out. I will likely update the .pyc suffix to
-typeguard400or similar once the final release is out. Until then, continue to clear the .pyc files when you update.Bump to typeguard 4+ to fix agronholm/typeguard#318
Update typeguard version to >=3.0.2
Update typeguard version to >=3.0.2
Updated Typeguard requirement to >=4.0.0. While Typeguard 3.x.x is 2 …