-
Notifications
You must be signed in to change notification settings - Fork 1
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
Typechecking fails for recursive type annotations #6
Comments
first of all, wow, hi! always nice to see someone use this old thing. i'm gonna be honest, i haven't touched this code or done any serious python in a while, and i'm not sure if i'll have the time to fix this anytime soon. i'll try, but i can't promise anything, so if this is a blocker, you should probably fork or look for another library. sorry! Fixing ityeah, recursive definitions are always tricky, i didn't need them for the thing i wrote this library for, so i never ran into this 😅 now, i believe the only thing we need the types for is to splice them into the Possible workaroundsyou could of course do something like
but really that's just disabling the typecheck, so it's not great. a slightly uglier (unless you like LISP :P) but typesafe solution would be to define your type "the namedtuple way":
(from the docstring here) iirc only the "class-based way" calls lastly, if THAT fails, you could define it as untyped and try wrapping the constructors in
but you'd need to get the annotations in there somehow. but yeah, again, i'm not sure if i'll have time/space to fix this -- i've been away from python stuff for quite some time, and i'm pretty busy ATM. if you've got time to dig into this, i'd be more than happy to merge a PR or even give you commit/PyPI access -- this library has been gathering dust for quite some time, it could use some love 😄 |
Well for the time being I did the really dumb thing and just replaced all recursive references with I only took a quick look into the insides of the library, and pretty quickly decided I didn't want to sidetrack my current project trying to work out a solution (this library was actually what introduced me to typeguard. I've used mypy before for typechecking, but that's about it), but depending on how things go, I might work on it later. It would probably be a good thing to get properly acquainted with typeguard. I do like Lisp, but I also prefer to keep my Lisp with my Lisp, and not inside my python 😄 Replacing the recursive references with Any was by far the easiest refactor I could do to work around things. Plus, since not all my sumtypes actually have recursive references, switching notation would result in either a mishmash of both styles, or end up causing a lot more work to switch every sumtype over to that syntax, so I took the lazy option. |
Consider the following:
This results in the following NameError exception being raised:
The future import is supposed to help avoid this problem by delaying the evaluation of all type annotations, but since you are explicitly calling typing.get_type_hints(), I believe you are effectively breaking that functionality. Further, using @typeguard_ignore does not fix this issue either.
This recursive definition is a common way to encode things like parse trees (which is exactly my current use case).
The text was updated successfully, but these errors were encountered: