You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mind inheritance when inferring TypedDict-based class #2876
I'll call this an "enhancement", and would be happy to have the feature.
But be careful! You'll need to traverse the MRO, use the closest registered supertype for all fields defined on that type (handling the case where the registered strategy doesn't return a dict...), and then use our current logic to resolve the fields added by subclasses of whatever was registered down to the type we're actually constructing.
Gosh... looks kinda ugly to me, but then I've never felt that TypedDict is really idiomatic for statically-typed-Python. If you're going to this much trouble to annotate your dicts, maybe just use a dataclass?
In any case, I'd hope that at runtime this will just use the existing attributes for lists of optional and required keys (see python/cpython#17214); in which case it will "just work" with Hypothesis.
Unfortunately, it turns out that the TypedDict implementation simply doesn't retain inheritance information at runtime, so we won't be able to support this feature.
I was keen enough to try parsing, but inspect.getsource()also fails!
Yeah, probably... I just don't have the energy for another long engagement with python-dev at the moment, and once I do it'll be about getting PBT running in their CI.
Huh, looks like inspect.getsource() works for TypedDict on Python 3.8 and later! (I'm still not keen to rely on this though...)
I think that TypedDict is a bit special / different to standard classes here, but on reflection that you're also right that we probably don't want this feature - it's pretty hard to say why e.g. dataclasses should follow either standard classes or typeddicts, and in that case we should default to treating them all the same (i.e. status quo).
And also, thanks again for following this up! It's fantastic to have your independent investigations and design work on this stuff 😍
Activity
Zac-HD commentedon Feb 25, 2021
I'll call this an "enhancement", and would be happy to have the feature.
But be careful! You'll need to traverse the MRO, use the closest registered supertype for all fields defined on that type (handling the case where the registered strategy doesn't return a dict...), and then use our current logic to resolve the fields added by subclasses of whatever was registered down to the type we're actually constructing.
Tricky, but doable 😁
sobolevn commentedon Mar 1, 2021
There's an ongoing
PEPdiscussion that might be related: https://mail.python.org/archives/list/typing-sig@python.org/thread/53XVOD5ZUKJ263MWA6AUPEA6J7LBBLNV/ But, since Guido is the PEP's sponsor - it is a matter of time.Maybe there are some things to consider from it in the current implementation.
Zac-HD commentedon Mar 1, 2021
Gosh... looks kinda ugly to me, but then I've never felt that
TypedDictis really idiomatic for statically-typed-Python. If you're going to this much trouble to annotate your dicts, maybe just use a dataclass?In any case, I'd hope that at runtime this will just use the existing attributes for lists of optional and required keys (see python/cpython#17214); in which case it will "just work" with Hypothesis.
gwerbin commentedon Mar 17, 2021
Zac-HD commentedon Dec 12, 2021
Unfortunately, it turns out that the
TypedDictimplementation simply doesn't retain inheritance information at runtime, so we won't be able to support this feature.I was keen enough to try parsing, but
inspect.getsource()also fails!sobolevn commentedon Dec 12, 2021
@Zac-HD I think it is worth filling a BPO issue. Maybe even two:
inspect.getsource()should not failI've seen lots of bugs with
TypedDictin terms of its creation / metaclass usage / etc. Many related PRs were accepted.Zac-HD commentedon Dec 12, 2021
Yeah, probably... I just don't have the energy for another long engagement with python-dev at the moment, and once I do it'll be about getting PBT running in their CI.
sobolevn commentedon Dec 12, 2021
Yes, I've seen this thread about hypothesis in CPython's CI. Sending hopes and prayers, really cool feature 🙏
I will open them in this case 🙂
sobolevn commentedon Dec 26, 2021
I am filling a BPO issue right now and I found that the original proposal by @Melevir is not what I would expect from hypothesis.
Let's see how regular classes work on this case:
This will fail with:
Why the same should work for
TypedDict?sobolevn commentedon Dec 26, 2021
@Zac-HD I was also not successful in reproducing
inspect.getsourcefailure.This works for me:
Is there anything that I am missing?
Zac-HD commentedon Dec 27, 2021
Huh, looks like
inspect.getsource()works forTypedDicton Python 3.8 and later! (I'm still not keen to rely on this though...)I think that
TypedDictis a bit special / different to standard classes here, but on reflection that you're also right that we probably don't want this feature - it's pretty hard to say why e.g. dataclasses should follow either standard classes or typeddicts, and in that case we should default to treating them all the same (i.e. status quo).And also, thanks again for following this up! It's fantastic to have your independent investigations and design work on this stuff 😍