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
I have a class-hierarchy and a matching class-hierarchy of managers for them:
from __future__ importannotationsfromtypingimportClassVar, Generic, TypeVar, reveal_typeclassDir:
defwalk(self) ->None: print("Dir")
classGit(Dir):
defwalk(self) ->None: print("Git")
defcommit(self) ->None: print("Commit")
T=TypeVar("T", bound=Dir)
classManager(Generic[T]):
create: ClassVar[type[T]] # def create(self) -> T: raise NotImplementedErrordefcreate2(self) ->tuple[T, T]: return (self.create(), self.create())
classDirManager(Manager[Dir]):
create=Dir# def create(self) -> Dir: return Dir()classGitManager(DirManager): # , Manager[Git]create=Git# def create(self) -> Git: return Git()reveal_type(DirManager().create())
# note: Revealed type is "mypy-generic-multiclass.Dir"reveal_type(DirManager().create2())
# note: Revealed type is "tuple[mypy-generic-multiclass.Dir, mypy-generic-multiclass.Dir]"reveal_type(GitManager().create())
# note: Revealed type is "mypy-generic-multiclass.Git"reveal_type(GitManager().create2())
# note: Revealed type is "tuple[mypy-generic-multiclass.Dir, mypy-generic-multiclass.Dir]"
Is it possible to get GitManager().create2() to also return (Git, Git) instead of (Dir, Dir) without explicitly overwriting create2() in every sub-class?
PS: pyright and pyrefly do not like the declaration of create: ClassVar[type[T]] and report
error: "ClassVar" type cannot include type variables (reportGeneralTypeIssues)
#1424 has some more information about that. Using the alternative def create(self) -> … as hinted in the comments does not make a difference regarding the return types. mypy and ty accept it. See #1775 for hinting that.
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.
-
I have a class-hierarchy and a matching class-hierarchy of managers for them:
Is it possible to get
GitManager().create2()to also return(Git, Git)instead of(Dir, Dir)without explicitly overwritingcreate2()in every sub-class?PS:
pyrightandpyreflydo not like the declaration of create:ClassVar[type[T]]and report#1424 has some more information about that. Using the alternative
def create(self) -> …as hinted in the comments does not make a difference regarding the return types.mypyandtyaccept it. See #1775 for hinting that.Beta Was this translation helpful? Give feedback.
All reactions