[Bug] PEP 484- and 585-compliant nested generics are broken af, bro
See original GitHub issueI think this will be easiest to understand from an example:
from beartype import beartype
RowIndex = int
ColIndex = int
@beartype
def fn(a: RowIndex) -> ColIndex:
return a
fn(RowIndex(1))
I want this call to fn()
to fail (due to beartype, obviously). Clearly it can’t be done in exactly the way I’ve written, so either this bit:
RowIndex = int
ColIndex = int
or this bit:
RowIndex(1)
must be done differently. I am open to any ideas! Of course we could write a class that subtypes int
, but that is extremely clumsy and annoying. I want to do the aliasing and the instantiation from the supertype (i.e. the RowIndex(1)
) in the least obtrusive way possible.
You could also use dataclasses, and then beartype will check the constructor, but then you have to do row_idx.val
every time you want the value, which is highly annoying.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
PEP 484 – Type Hints - Python Enhancement Proposals
This behavior is called “type erasure”; it is common practice in languages with generics (e.g. Java, TypeScript). Using generic classes (parameterized or not) ......
Read more >error with nested generics · Issue #16709 · vlang/v - GitHub
error : the parameter type name of a generic struct, must be a single capital letter placeholder name, like T or X, or...
Read more >Nested generic types not allowed? | Apple Developer Forums
The above fails to compile with the error message: error: generic type 'Node' nested in type 'LinkedListStack' is not allowed.
Read more >c# - Nested Generics: Why can't the compiler infer the type ...
The last example produces a compiler error, but it seems to me that it should be able to infer the type arguments without...
Read more >How does training time scale w.r.t. model size? - Deepmind/Ferminet
vertical/horizontal classes wrong, 1, 2017-06-16, 2022-07-23. [Bug] PEP 484- and 585-compliant nested generics are broken af, bro, 7, 2022-06-24, 2022-11-04.
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
Wow, my record is currently
0-2
for claiming things are true ‘no doubt’ this week. I should really stop saying that, lol.Pleased to hear this is something you intend to support! Yeah, I had imported
List
withfrom beartype.typing import List
so it was using the 585-style hints under the hood. No exception raised, as you said.Sadly, there is this
No doubt you are not supposed to subclass subscripted type hints. But it’s too bad this does not work.
The other annoyance is that anyone using
black
to autoformat code will not be able to define these aliases/classes in one line.