Movatterモバイル変換


[0]ホーム

URL:


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 inGeneral
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 TwistedDeferred, asyncio or concurrentFuture, 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:

@dataclassclassMyDataClass:foo:intbar:strbag_dataclass:Bag[MyDataClass]=Bag[MyDataClass](MyDataClass(foo=1,bar='x'))# Happy path examplesbag_dataclass_foo:Bag[int]=bag_dataclass.foofoo:int=bag_dataclass.foo.get()foo_:int=bag_dataclass_foo.get()bag_dataclass_bar:Bag[str]=bag_dataclass.barbar:str=bag_dataclass.bar.get()bar_:str=bag_dataclass_bar.get()classMyTypedDict(TypedDict):foo:intbar:strbag_typeddict:Bag[MyTypedDict]=Bag[MyTypedDict]({'foo':1,'bar':'x'})# Happy path examplesbag_typeddict_foo:Bag[int]=bag_typeddict.foodfoo:int=bag_typeddict.foo.get()dfoo_:int=bag_typeddict_foo.get()bag_typeddict_bar:Bag[str]=bag_typeddict.bardbar:str=bag_typeddict.bar.get()dbar_:str=bag_typeddict_bar.get()

I've got thisworkingwith amypy 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 potentialProxy type? Likedef __getattr__(self, val: str) -> Proxy[MyDataClass]: and thenreveal_type(bag_typeddict.bar) would reveal"builtins.str".

You must be logged in to vote
3 replies
@cjw296
Comment options

Did any progress happen onProxy types?

@cjw296
Comment options

nudge on this again...

@cjw296
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
@sobolevn
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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@cjw296@sobolevn

[8]ページ先頭

©2009-2025 Movatter.jp