-
Notifications
You must be signed in to change notification settings - Fork 25
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
More issues with Annotated #123
Comments
MWE In [1]: import sys
...: from beartype.vale import Is
...: from plum import dispatch, Signature
...: from typing_extensions import Annotated
...:
...: class MyClass():
...: def __init__(self) -> None:
...: self.special_attr = 'special_value'
...: class AnotherClass():
...: pass # notable difference: no special_attr
...:
...: class_type_check = Annotated[object, Is[lambda value: hasattr(value,'special_attr')]]
...:
...: def g(value: class_type_check):
...: if hasattr(value,'special_attr'):
...: print(f'Value has special_attr={value.special_attr}')
...: else:
...: print(f'ERROR: This code should not be reached- 1')
...:
...: from plum.signature import resolve_pep563
...:
...: print(g.__annotations__)
...: resolve_pep563(g)
...: print(g.__annotations__)
...:
{'value': typing.Annotated[object, Is[lambda value: hasattr(value, 'special_attr')]]}
{'value': <class 'object'>} it's our logic in def resolve_pep563(f: Callable):
"""Utility function to resolve PEP563-style annotations and make editable.
This function mutates `f`.
Args:
f (Callable): Function whose annotations should be resolved.
"""
if hasattr(f, "__annotations__"):
beartype_resolve_pep563(f) # This mutates `f`.
# Override the `__annotations__` attribute, since `resolve_pep563` modifies
# `f` too.
for k, v in typing.get_type_hints(f).items():
f.__annotations__[k] = v that breaks. Not beartype. However, I really wonder if we can/should support this kind of type hints with plum... |
Digging deeper, the issue is with def g(value: class_type_check):
if hasattr(value,'special_attr'):
print(f'Value has special_attr={value.special_attr}')
else:
print(f'ERROR: This code should not be reached- 1')
typing.get_type_hints(g)
{'value': object}
`` |
...heh. I have been summoned and shall now dispense justice with the bear claw. Repeat after me, everyone:
Indeed, the This is why @beartype internally defines its own private utility functions rather than ever call anything published by I'm kinda unsure why Plum is even calling for k, v in typing.get_type_hints(f).items():
f.__annotations__[k] = v Ideally, that should be a noop. Does that actually do something meaningful? I rub my chin thoughtfully. |
Hmm, I believe the original motivation for calling # None of this!
# from __future__ import annotations
from typing import get_type_hints
class A:
def f(self, x: "A"):
return x
a = A()
print(get_type_hints(a.f))
# {'x': <class '__main__.A'>} @leycec, does Beartype happen to implement a utility function that would be superior replacement for |
* added failing test case for annotated * Fix issues with Annotated * fix failing CI checks * cleaned up pr --------- Co-authored-by: Samuel Hodges <[email protected]> Co-authored-by: Wessel <[email protected]>
With @hodgespodge recent PR #126, this is now fixed: from typing import Annotated
from beartype.vale import Is
from plum import dispatch
class SpecialClass:
def __init__(self) -> None:
self.special_attr = "special_value"
class NormalClass:
pass
@dispatch
def f(value: Annotated[object, Is[lambda value: hasattr(value, "special_attr")]]):
print("Value has `special_attr` defined!")
f(SpecialClass())
f(NormalClass()) Also note the pretty output due to @PhilipVinc's work. :D |
OMG. I so dropped the ball on this one, @wesselb:
I... totally missed that desperate request. The answer, of course, is: "Probably! @beartype probably did have exactly what you needed!" Which is a shame, too. Dynamic evaluation of arbitrary forward references at runtime is really non-trivial. But... you must have already done it here, somehow. You made miracles happen without us. I'm so sorry. @beartype definitely should have done that for Plum. If anyone's curious, @beartype has extensive facilities for resolving forward references in its currently private The most useful for Plum purposes would almost certainly be the The Yar, I Speak Like a Pirate Now.Incidentally, are we all aware that PEP 563 has now officially been deprecated and will be replaced by something that's probably equally broken called "annotation scopes" in Python ≥ 3.13 or 3.14, I think? I've been fighting annotation scopes throughout the @beartype 0.17.0 release cycle. So, I can confidently be the harbinger of doom: "Thare be trouble ahead, yar." In fact, annotation scopes are so busted that I'm gonna have to push out a mass email to the official CPython Like, breaking PyTorch and ChatGPT scale of ugly stuff. Incredibly ugly stuff is what I'm saying. Sucks, honestly. Can't believe CPython is doing this after the PEP 563 debacle but... here we go again. |
@leycec Nonono, you've got so much on your hands—there's absolutely no obligation to reply to every little request. :)
Aha, this is amazing. Yes, it looks like
Dear god, I don't even want to know. 😭 Why must typing in Python be so painful... Well, I hope your mass email reaches the right people. |
Superbness! Just let me know what sort of a public API Plum could benefit from and we'll make this magical unicorn happen. I'm unsure if @beartype currently has an exact analogue to This is why I play video games before passing out. We didn't get a winter here in Canada, so Nordic skiing is right out. "Why so much freezing rain!?," I cry. Hope you're having an awesome winter in Amsterdam, though! May Plum live long and prosper. 🖖 |
@leycec That's fantastic. :) Will definitely do. I think we don't need an exact analogue of By the way, very happy birthday!!! (And a massive congrats with the 0.17.0 release! :)) I hope you're having an awesome and relaxing day today. 😌 |
Thanks so much for the kindest words from what is probably the Amsterdam coffee shop I wish I was inhabiting. Let me know if I can do anything to improve either the internal API or documentation of Oh – and please do let me know which currently private functions and/or types you eventually settle on calling. To avoid breaking Plum, I'd like to "quietly" publicize those somewhere – probably in |
@leycec Will definitely do! :) I haven't yet had the opportunity to do this, but will let you know as soon as I do. |
Howdy again y'all.
I'm having issues with Annotated that lead me to believe issue #120 was not actually resolved.
The text was updated successfully, but these errors were encountered: