Use case for typing.Type with abstract types
In https://github.com/python/mypy/issues/2169 and https://github.com/python/mypy/issues/1843 there was discussion about using `Type[some_abc]` and how it should be allowed in a function signature, but that the call-site was expected to pass a concrete subclass of `some_abc`. There is an implicit assumption that the objective of a function taking such a type is, ultimately, to instantiate the type. @gvanrossum said, in https://github.com/python/mypy/issues/2169#issuecomment-249001710: > But maybe we need a check that you call this thing with a concrete subclass; > and for that we would need some additional notation (unless maybe we could > just say that whenever there's an argument of Type[A] where A is abstract, > that the argument must be a concrete subclass. But for that we'd need some > experiment to see if there's much real-world code that passes abstract > classes around. (If there is, we'd need to have a way to indicate the need > in the signature.) I have such a use case. I have a sequence of observers supplied by clients of my library, to which I want to dispatch events according to the abstract base class(es) that each implements. I tried to do this as follows: ```python import abc import typing class FooObserver(metaclass=abc.ABCMeta): """Receives Foo events.""" @abc.abstractmethod def on_foo(self, count: int) -> None: raise NotImplementedError() class BarObserver(metaclass=abc.ABCMeta): """Receives Bar events.""" @abc.abstractme
Posted by erikwright on Mar 11, 2018, 3:46 AM
Tags
Related issues
- [Meta] Feedback on granular control over Copilot changes
- unexpected status 401 Unauthorized issue
- [MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
- [CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
- Video stops playing after some minutes
- [MODEL] Claude verbose code comments by default — ignores instructions to stop.
Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments
github (Software)
[Meta] Feedback on granular control over Copilot changes
unexpected status 401 Unauthorized issue
[MODEL] Measurable quality regression in Claude generation 5 (Fable 5 / Opus 5 / Sonnet 5): worse nonsense detection, ~2x verbosity, under-disclosed model fallb
[CRITICAL] Subagent spawning and subagent pattern bugs trigger infinite recursion, infinite token usage, grossly inefficient token usage, and lost accumulated s
Video stops playing after some minutes
[MODEL] Claude verbose code comments by default — ignores instructions to stop.
Scope Codex VS Code chats to the current workspace/project
Update on "Co-authored-by: Copilot" in commit messages
Use case for typing.Type with abstract types
In https://github.com/python/mypy/issues/2169 and https://github.com/python/mypy/issues/1843 there was discussion about using `Type[some_abc]` and how it should be allowed in a function signature, but that the call-site was expected to pass a concrete subclass of `some_abc`. There is an implicit assumption that the objective of a function taking such a type is, ultimately, to instantiate the type. @gvanrossum said, in https://github.com/python/mypy/issues/2169#issuecomment-249001710: > But maybe we need a check that you call this thing with a concrete subclass; > and for that we would need some additional notation (unless maybe we could > just say that whenever there's an argument of Type[A] where A is abstract, > that the argument must be a concrete subclass. But for that we'd need some > experiment to see if there's much real-world code that passes abstract > classes around. (If there is, we'd need to have a way to indicate the need > in the signature.) I have such a use case. I have a sequence of observers supplied by clients of my library, to which I want to dispatch events according to the abstract base class(es) that each implements. I tried to do this as follows: ```python import abc import typing class FooObserver(metaclass=abc.ABCMeta): """Receives Foo events.""" @abc.abstractmethod def on_foo(self, count: int) -> None: raise NotImplementedError() class BarObserver(metaclass=abc.ABCMeta): """Receives Bar events.""" @abc.abstractme
Comments (0)
No comments yet. Start the discussion.