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
What is it about str and Sequence[T] that they don't work with your generic class implementation? Or more importantly, what is it about other types that allow them to work with your implementation? If you can answer that question, you should be able to describe it using a protocol and use that protocol as an upper bound for T_co. That way, a type checker would generate an error for any type that doesn't meet the requirements of the protocol.
The way I'm reading the original question I'm guessing that he's trying to implement something like an SQLAlchemy ORM or pandas DataFrame where you can use operators to build expressions that can then later be compiled/evaluated inside a query such as Foo.a < 20, but some of the operations are only valid for a subset of the possible data types of Field.
One possible solution would be to create subclasses with those specific restrictions and add overloads to __init__ to retrieve the correct subclass. The disadvantage of that approach would be that those operations would then not be available on the base class, so Field[Any] would probably end up being annoying to use, so you would still be better of with something like a type_error decorator to mark the operations that become invalid once you specialize the Field. It might however be possible to provide an upper bound for TArg instead.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
Is it possible to have typing errors when calling a generic class method with a specific specialized type?
It's possible to enable a method for a specific type, but I want to disable it.
Example (enable method only for
strgeneric specialization)My current workaround (return
Nonefor specific generic specializations):But I would like to get
typingerror instead ofNoneas return type.All reactions