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

gh-104050: Argument clinic: improve typing around adding C converters by AlexWaygood · Pull Request #107209 · 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
20 changes: 13 additions & 7 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 @@ -44,6 +44,7 @@
NoReturn,
Protocol,
TypeGuard,
TypeVar,
overload,
)

Expand Down Expand Up @@ -2647,10 +2648,12 @@ def __getattribute__(self, name: str) -> Any:
fail("Stepped on a land mine, trying to access attribute " + repr(name) + ":\n" + self.__message__)


CConverterClassT = TypeVar("CConverterClassT", bound=type["CConverter"])

def add_c_converter(
f: type[CConverter],
f: CConverterClassT,
name: str | None = None
) -> type[CConverter]:
) -> CConverterClassT:
Comment on lines +2651 to +2656

AlexWaygood Jul 24, 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

By using a TypeVar here (and with the other functions in this PR), we can make the annotation much more expressive.

The annotation currently says "you put in an object f that's some subtype of type[CConverter], and you get out an object that's some subtype of type[CConverter].

The new annotation, by contrast, says, "you put in an object f, and you get out an object that has the same type as whatever the type of f is, as long as the type of f is a subtype of type[CConverter].

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

As an example, with the following snippet:

x = add_c_converter(bool_converter)

Mypy would have inferred the type of x to be type[CConverter] with the current annotation. But with the new annotation, mypy should be able to infer that the type of x is bool_converter.

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

Neat!

if not name:
name = f.__name__
if not name.endswith('_converter'):
Expand All @@ -2659,7 +2662,7 @@ def add_c_converter(
converters[name] = f
return f

def add_default_legacy_c_converter(cls):
def add_default_legacy_c_converter(cls: CConverterClassT) -> CConverterClassT:
# automatically add converter for default format unit
# (but without stomping on the existing one if it's already
# set, in case you subclass)
Expand All @@ -2670,16 +2673,19 @@ def add_default_legacy_c_converter(cls):

def add_legacy_c_converter(
format_unit: str,
**kwargs
) -> Callable[[ConverterType], ConverterType]:
**kwargs: Any
) -> Callable[[CConverterClassT], CConverterClassT]:
"""
Adds a legacy converter.
"""
def closure(f):
def closure(f: CConverterClassT) -> CConverterClassT:
added_f: Callable[..., CConverter]
if not kwargs:
added_f = f
else:
added_f = functools.partial(f, **kwargs)
# mypy's special-casing for functools.partial
# can't quite grapple with this code here
added_f = functools.partial(f, **kwargs) # type: ignore[arg-type]
if format_unit:
legacy_converters[format_unit] = added_f
return f
Expand Down

Back | FazBrowse Home | New Git URL