GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||
GeezerVison is showing github.com — gh-104050: Argument clinic: more misc typing improvements by AlexWaygood · Pull Request #107264 · python/cpython Conversation
|
| def __init__( | ||
| cls, name: str, bases: tuple[type, ...], classdict: dict[str, Any] | ||
| ) -> None: | ||
| converter_cls = cast(type["CConverter"], cls) | ||
| add_c_converter(converter_cls) | ||
| add_default_legacy_c_converter(converter_cls) |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Mypy was complaining because our type annotation for add_c_converter says (correctly) that the argument passed in has to be a subclass of CConverter. But that isn't necessarily true here, since this metaclass could theoretically be used with classes other than CConverter (it isn't, and it won't be, but mypy doesn't know that).
Ideally we could do this:
def __init__( cls, name: str, bases: tuple[type, ...], classdict: dict[str, Any] ) -> None: assert issubclass(cls, CConverter) add_c_converter(cls) add_default_legacy_c_converter(cls)
But we can't... because if cls is CConverter itself, then at this point, CConverter doesn't exist in the global namespace yet! So that would fail with NameError.
I therefore elected to use typing.cast() to just tell mypy to go away here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for writing these excellent explanations!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm glad they're all making sense! :D
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.