question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] PEP 484- and 585-compliant nested generics are broken af, bro

See original GitHub issue

I 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:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
langfieldcommented, Jun 27, 2022

Ohgodnoes. Actually, you are supposed to subclass subscripted type hints. You’re absolutely in the right and fully compliant with typing standards like PEP 484 and 585 here. You’re doing good things.

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 with from beartype.typing import List so it was using the 585-style hints under the hood. No exception raised, as you said.

1reaction
langfieldcommented, Jun 25, 2022

Sadly, there is this

class Board(List[List[str]]): pass

@beartype
def get_board() -> Board:
    return Board([1])

get_board()

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found