Wow, that was fast. Thanks to @STerliakov for finding a related post!
The solution is generics. So the BusinessMan
declaration becomes
class BusinessMan[BriefcaseType: Briefcase]:
# snip
@classmethod
def from_briefcase(cls, briefcase: BriefcaseType) -> Self:
# snip
which indicates that from_briefcase()
takes a BriefcaseType
, which is any subclass of Briefcase
.
The declaration for BiggerBusinessMan
becomes
class BiggerBusinessMan(BusinessMan[BiggerBriefcase]):
# snip
@classmethod
def from_briefcase(cls, briefcase: BiggerBriefcase) -> Self:
# snip
which says that BiggerBusinessMan
inherits from the variant of BusinessMan
where BriefcaseType
is BiggerBriefcase
.
Type annotations are sublime.
Pleasure doing business!