FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-104050: Argument clinic: more misc typing improvements by AlexWaygood · Pull Request #107264 · python/cpython · GitHub

/ cpython Public

gh-104050: Argument clinic: more misc typing improvements - #107264

Merged
AlexWaygood merged 1 commit into
python:mainfrom
AlexWaygood:misc-clinic-typing
Jul 25, 2023
Merged

gh-104050: Argument clinic: more misc typing improvements#107264
AlexWaygood merged 1 commit into
python:mainfrom
AlexWaygood:misc-clinic-typing

Conversation

AlexWaygood commented Jul 25, 2023
edited by bedevere-bot
Loading

Copy link
Copy Markdown
Member

Comment thread Tools/clinic/clinic.py
Comment on lines +2698 to +2703
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)

AlexWaygood Jul 25, 2023
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks for writing these excellent explanations!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'm glad they're all making sense! :D

AlexWaygood merged commit ee90107 into python:main Jul 25, 2023
AlexWaygood deleted the misc-clinic-typing branch July 25, 2023 21:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL