Maybe bug: Creating diamond diagram using NewType #5524
Replies: 2 comments 8 replies
-
When you create a class Foo(int | str): ... # Illegal! Keep in mind that unions are not ordered, so # These are very different classes!
class Sub1(int, str): ...
class Sub2(str, int): ... I don't understand what you're trying to achieve by creating "a diamond-diamond subtype for type checking without creating an actual subclass". There's potentially another way to accomplish your goal, but you'd have to explain more for me to offer a suggestion. |
Beta Was this translation helpful? Give feedback.
-
Relatedly, I've also found a bit about |
Beta Was this translation helpful? Give feedback.
-
I tried to use
NewType
to create a diamond-inheritance subtype, similar to the followingAs mentioned in #1397, that's verboten. And indeed, pyright/pylance say
Expected class as second argument to NewType
. But why is that the case? The second argument toNewType
doesn't receive much detail in the docs (also), other than referring to it as a type. And PEP 483 says(although perhaps typing docs mangles the distinction between classes and types with "treat the new type as if it were a subclass [not subtype?] of the original type")
Am I using the wrong tool for the job? Is there not currently a way to create a diamond-diagram subtype for type checking without creating an actual subclass?
Beta Was this translation helpful? Give feedback.
All reactions