-
Notifications
You must be signed in to change notification settings - Fork 287
-
I think it's a relatively common pattern to have some kind of object that represents a value at a later date, or some other kind of "wrapping" type, such as a Twisted Deferred, asyncio or concurrent Future, etc. But how do we get type checking of the results of getting attributes of these?
Concretely:
https://github.com/cjw296/generative_typing/blob/main/bag.py
...which can be used like:
@dataclass class MyDataClass: foo: int bar: str bag_dataclass: Bag[MyDataClass] = Bag[MyDataClass](MyDataClass(foo=1, bar='x')) # Happy path examples bag_dataclass_foo: Bag[int] = bag_dataclass.foo foo: int = bag_dataclass.foo.get() foo_: int = bag_dataclass_foo.get() bag_dataclass_bar: Bag[str] = bag_dataclass.bar bar: str = bag_dataclass.bar.get() bar_: str = bag_dataclass_bar.get() class MyTypedDict(TypedDict): foo: int bar: str bag_typeddict: Bag[MyTypedDict] = Bag[MyTypedDict]({'foo': 1, 'bar': 'x'}) # Happy path examples bag_typeddict_foo: Bag[int] = bag_typeddict.foo dfoo: int = bag_typeddict.foo.get() dfoo_: int = bag_typeddict_foo.get() bag_typeddict_bar: Bag[str] = bag_typeddict.bar dbar: str = bag_typeddict.bar.get() dbar_: str = bag_typeddict_bar.get()
I've got this working with a mypy plugin, but how could I get this to work so that pylance, ruff, etc would also work?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 4 replies
-
Nope, __getattr__ support without a plugin is not possible at the moment.
However, this pattern is quite common.
Maybe it is time to start a discussion about potential Proxy type? Like def __getattr__(self, val: str) -> Proxy[MyDataClass]: and then reveal_type(bag_typeddict.bar) would reveal "builtins.str".
Beta Was this translation helpful? Give feedback.
All reactions
-
Did any progress happen on Proxy types?
Beta Was this translation helpful? Give feedback.
All reactions
-
nudge on this again...
Beta Was this translation helpful? Give feedback.
All reactions
-
#1775 seems to be the same ballpark as this...
Beta Was this translation helpful? Give feedback.
All reactions
-
@sobolevn - I guess that's what I'd feared... do pylance and friends support mypy plugins?
Beta Was this translation helpful? Give feedback.
All reactions
-
Nope :(
Only mypy can do this.
Beta Was this translation helpful? Give feedback.
All reactions
-
😕 1
-
Doing the same for the __getitem__ path seems even harder :-/
Beta Was this translation helpful? Give feedback.