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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 11 additions & 6 deletions Tools/clinic/clinic.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
Protocol,
TypeGuard,
TypeVar,
cast,
overload,
)

Expand Down Expand Up @@ -2694,9 +2695,12 @@ def closure(f: CConverterClassT) -> CConverterClassT:
return closure

class CConverterAutoRegister(type):
def __init__(cls, name, bases, classdict):
add_c_converter(cls)
add_default_legacy_c_converter(cls)
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)
Comment on lines +2698 to +2703

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


class CConverter(metaclass=CConverterAutoRegister):
"""
Expand Down Expand Up @@ -3174,10 +3178,10 @@ class defining_class_converter(CConverter):
def converter_init(self, *, type: str | None = None) -> None:
self.specified_type = type

def render(self, parameter, data) -> None:
def render(self, parameter: Parameter, data: CRenderData) -> None:
self._render_self(parameter, data)

def set_template_dict(self, template_dict):
def set_template_dict(self, template_dict: TemplateDict) -> None:
template_dict['defining_class_name'] = self.name


Expand Down Expand Up @@ -4048,7 +4052,7 @@ def parser_type(self) -> str:
assert isinstance(self.function, Function)
return required_type_for_self_for_parser(self.function) or self.type

def render(self, parameter, data):
def render(self, parameter: Parameter, data: CRenderData) -> None:
"""
parameter is a clinic.Parameter instance.
data is a CRenderData instance.
Expand All @@ -4064,6 +4068,7 @@ def render(self, parameter, data):
# because we render parameters in order, and self is always first.
assert len(data.impl_arguments) == 1
assert data.impl_arguments[0] == self.name
assert self.type is not None
data.impl_arguments[0] = '(' + self.type + ")" + data.impl_arguments[0]

def set_template_dict(self, template_dict: TemplateDict) -> None:
Expand Down

Back | FazBrowse Home | New Git URL