[Feature Request] Deeply type-check `set` and `frozenset` containers
See original GitHub issueI’ve only just noticed that the README
says these are only shallowly-checked. I’m very surprised, as these seem like bread-and-butter data structures. Is it accurate to say that whenever we write x: Set[T]
(for some specific T
) it really just checks against x: Set[Any]
?
What’s the roadmap look like for getting this implemented/merged? I’d love to look into how much work a PR for this would be.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Issues · beartype/beartype
The Future Sound of Beartype: Going Deep on Type-checking. #7 opened on Dec 9, ... [Feature Request] Deeply type-check set and frozenset containers....
Read more >python/typing - Gitter
But this is a popular feature request ... simple wrapper over a Python set for items with EqTypeClass" values: FrozenSet[EqTypeClass] def __contains__(self, ...
Read more >PEP 484 – Type Hints - Python Enhancement Proposals
Since type information about objects kept in containers cannot be ... The call is valid but the type variable AnyStr will be set...
Read more >frozenset() in Python
Python frozenset() Method creates an immutable Set object from an iterable. ... Return : Returns an equivalent frozenset object.
Read more >beartype
A deep type-check that an item randomly selected from that container itself satisfies the first check. For example, given a parameter's type hint...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Agh! I am being even more confusing than usual. Yes,
dict[str]
is literally the PEP 585 type hintdict[str]
. By dispatching on type hints, Plum is (basically) building out a runtime implementation of the PEP 484@typing.overload
decorator that actually does something useful: multiple dispatch, yo!Admittedly, I’ve never actually used multiple dispatch before. I just blindly throw around CompSci jargon like I know what I’m talking about. But now that I know about this Plum thing, I’ll be multiple-dispatching everything everywhere regardless of whether I need to. Object-oriented inheritance is so passé. 😉
EDIT: …oh. You were gently pointing out that
dict[str]
is invalid syntax. Of course, I am now facepalming the poor cat. Let’s pretend I wrote something valid likedict[str, str]
orset[str]
instead. This never happened, @langfield.</awkward_moments>
Checking them isn’t the hard part, the hard part is doing it in amortized
O(1)
time. The way beartype checks an arbitrary data structure without checking time being dependent on the size of the structure is to randomly pick a single element to check each time. That way repeated executions will gradually cover the whole space. But for sets and dicts we cannot sample a random element, at least without iterating through everything before that. Issue #7 has more info on all this.I wonder if there’s a way to do this via
ctypes
abuse - it’s going to be implementation-specific regardless, may as well use that.