Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can you do wrapping generic types properly without a mypy plugin? #1500

cjw296 started this conversation in General
Discussion options

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?

You must be logged in to vote

Replies: 3 comments 4 replies

Comment options

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".

You must be logged in to vote
3 replies
Comment options

Did any progress happen on Proxy types?

Comment options

nudge on this again...

Comment options

#1775 seems to be the same ballpark as this...

Comment options

@sobolevn - I guess that's what I'd feared... do pylance and friends support mypy plugins?

You must be logged in to vote
1 reply
Comment options

Nope :(
Only mypy can do this.

Comment options

Doing the same for the __getitem__ path seems even harder :-/

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /