Revision history [back]
As written, obj.to(Apple)
doesn't work because def to(self, subclass:'Apple')-> 'Apple':
matches when subclass is an instance of Apple not the type object for the Apple class. I suspect you want to use def to(self, subclass: typing.Type['Apple'])-> 'Apple'
so that subclass is the type object of the Apple class.
Unfortunately, Wing doesn't not find the right overload if you use subclass: typing.Type['Apple']
. This is a bug in Wing and we'll try to fix it soon.
Note that probably a better way to write this is
def to[T: ('Apple', 'Orange')](self, subclass: typing.Type[T]) -> T | None:
This version declares a type parameter that's constrained to Apple or Orange and also specifies that None may be returned. It version uses 3.12+ syntax, but could be written using older syntax. Unfortunately, Wing doesn't determine the correct return type when this version is used; this is also a bug that we'll try to fix.