Learned how easy it is to make something subscriptable with generics #464
Replies: 1 comment
-
|
lol. Yup. It's even easier than that under Python ≥ 3.12, where you no longer need to explicitly declare your type variables (i.e., from beartype.typing import Generic
class Something[_T](Generic[_T]): # <-- look, ma! no "TypeVar"
data: _T
def __init__(self, data: _T = None) -> None:
self.data = data
s = Something[int](5)
s.data # Type checker shows int! Even when I obscure my int value!The runtime type-checking issue, of course, is that no runtime type-checker (including @beartype) propagates generic subscriptions. That is, @beartype currently fails to "copy" the In fact, the core @beartype: We didn't make crazy. We just monkey-patch the crazy away. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@leycec Am I doing it wrong or is it really this non-trivial to subscript my types?!
Beta Was this translation helpful? Give feedback.
All reactions