-
Notifications
You must be signed in to change notification settings - Fork 86
Use dataclass to add type annotations to Extension so that it can easily be extended in setuptools.
#373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abravalheri
wants to merge
9
commits into
pypa:main
Choose a base branch
from
abravalheri:type-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Use dataclass to add type annotations to Extension so that it can easily be extended in setuptools.
#373
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
771a2ce
Remove unused type ignore mark
abravalheri 4bed279
Use dataclass for typing Extension
abravalheri 37447a4
Separate __post_init__
abravalheri 0bb738e
Add compat.py310.dataclass_transform fallback
abravalheri 1563042
Add a dataclass-like decorator that is lenient towards extra arguments
abravalheri dab5c11
Use lenient_dataclass to simplify Extension class
abravalheri e5c817b
Remove the now unecessary explicit call to __post_init__
abravalheri a24af92
Add comment about what to do when backward compatibility is not needed.
abravalheri 7a083da
Add sanity check for type inference on Extension
abravalheri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # This is a private module, but setuptools has the explicit permission to use it. | ||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from dataclasses import dataclass, fields | ||
| from functools import wraps | ||
| from typing import TypeVar | ||
|
|
||
| from .compat.py310 import dataclass_transform | ||
|
|
||
| _T = TypeVar("_T", bound=type) | ||
|
|
||
|
|
||
| @dataclass_transform() | ||
| def lenient_dataclass(**dc_kwargs): | ||
| """ | ||
| Problem this class intends to solve: | ||
| - We need to modify __init__ so to achieve backwards compatibility | ||
| and keep allowing arbitrary keywords to be ignored | ||
| - But we don't want to throw away the dataclass-generated __init__ | ||
| specially because we don't want to have to redefine all the typing | ||
| for the function arguments | ||
|
|
||
| If/when lenient behaviour and backward compatibility are no longer needed, | ||
| the whole customization can be removed. | ||
| A regular ``dataclass`` could be used instead. | ||
| """ | ||
|
|
||
| @wraps(dataclass) | ||
| def _wrap(cls: _T) -> _T: | ||
| cls = dataclass(**dc_kwargs)(cls) | ||
| # Allowed field names in order | ||
| safe = tuple(f.name for f in fields(cls)) | ||
| orig_init = cls.__init__ | ||
|
|
||
| @wraps(orig_init) | ||
| def _wrapped_init(self, *args, **kwargs): | ||
| extra = {repr(k): kwargs.pop(k) for k in tuple(kwargs) if k not in safe} | ||
| if extra: | ||
| msg = f""" | ||
| Please remove unknown `{cls.__name__}` options: {','.join(extra)} | ||
| this kind of usage is deprecated and may cause errors in the future. | ||
| """ | ||
| warnings.warn(msg) | ||
|
|
||
| # Ensure default values (e.g. []) are used instead of None: | ||
| positional = {k: v for k, v in zip(safe, args) if v is not None} | ||
| keywords = {k: v for k, v in kwargs.items() if v is not None} | ||
| return orig_init(self, **positional, **keywords) | ||
|
|
||
| cls.__init__ = _wrapped_init | ||
| return cls | ||
|
|
||
| return _wrap | ||
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from typing import TYPE_CHECKING, Any, Callable, TypeVar | ||
|
|
||
| _T = TypeVar("_T") | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| from typing import dataclass_transform | ||
| else: | ||
| if TYPE_CHECKING: | ||
| # typing_extensions usually "exist" when type-checking, | ||
| # without the need for extra runtime dependencies | ||
| from typing_extensions import dataclass_transform | ||
| else: | ||
| # Runtime no-op | ||
| def dataclass_transform( # type: ignore[misc] | ||
| *, | ||
| eq_default: bool | None = None, | ||
| order_default: bool | None = None, | ||
| kw_only_default: bool | None = None, | ||
| field_specifiers: tuple[type[Any], ...] = (), | ||
| **_: Any, | ||
| ) -> Callable[[_T], _T]: | ||
| def _decorator(obj: _T) -> _T: | ||
| return obj | ||
|
|
||
| return _decorator |
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 |
|---|---|---|
|
|
@@ -70,7 +70,7 @@ class Compiler: | |
| # dictionary (see below -- used by the 'new_compiler()' factory | ||
| # function) -- authors of new compiler interface classes are | ||
| # responsible for updating 'compiler_class'! | ||
| compiler_type: ClassVar[str] = None # type: ignore[assignment] | ||
| compiler_type: ClassVar[str] = None | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #374 |
||
|
|
||
| # XXX things not handled by this compiler abstraction model: | ||
| # * client can't provide additional options for a compiler, | ||
|
|
||
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
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 |
|---|---|---|
|
|
@@ -6,8 +6,10 @@ | |
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import warnings | ||
| from collections.abc import Iterable | ||
| from dataclasses import field, fields | ||
|
|
||
| from ._dataclass import lenient_dataclass | ||
|
|
||
| # This class is really only used by the "build_ext" command, so it might | ||
| # make sense to put it in distutils.command.build_ext. However, that | ||
|
|
@@ -20,137 +22,133 @@ | |
| # order to do anything. | ||
|
|
||
|
|
||
| @lenient_dataclass() | ||
| class Extension: | ||
| """Just a collection of attributes that describes an extension | ||
| module and everything needed to build it (hopefully in a portable | ||
| way, but there are hooks that let you be as unportable as you need). | ||
| """ | ||
|
|
||
| name: str | ||
| """ | ||
| the full name of the extension, including any packages -- ie. | ||
| *not* a filename or pathname, but Python dotted name | ||
| """ | ||
|
|
||
| sources: Iterable[str | os.PathLike[str]] | ||
| """ | ||
| iterable of source filenames (except strings, which could be misinterpreted | ||
| as a single filename), relative to the distribution root (where the setup | ||
| script lives), in Unix form (slash-separated) for portability. Can be any | ||
| non-string iterable (list, tuple, set, etc.) containing strings or | ||
| PathLike objects. Source files may be C, C++, SWIG (.i), platform-specific | ||
| resource files, or whatever else is recognized by the "build_ext" command | ||
| as source for a Python extension. | ||
| """ | ||
|
|
||
| include_dirs: list[str] = field(default_factory=list) | ||
| """ | ||
| list of directories to search for C/C++ header files (in Unix | ||
| form for portability) | ||
| """ | ||
|
|
||
| Instance attributes: | ||
| name : string | ||
| the full name of the extension, including any packages -- ie. | ||
| *not* a filename or pathname, but Python dotted name | ||
| sources : Iterable[string | os.PathLike] | ||
| iterable of source filenames (except strings, which could be misinterpreted | ||
| as a single filename), relative to the distribution root (where the setup | ||
| script lives), in Unix form (slash-separated) for portability. Can be any | ||
| non-string iterable (list, tuple, set, etc.) containing strings or | ||
| PathLike objects. Source files may be C, C++, SWIG (.i), platform-specific | ||
| resource files, or whatever else is recognized by the "build_ext" command | ||
| as source for a Python extension. | ||
| include_dirs : [string] | ||
| list of directories to search for C/C++ header files (in Unix | ||
| form for portability) | ||
| define_macros : [(name : string, value : string|None)] | ||
| list of macros to define; each macro is defined using a 2-tuple, | ||
| where 'value' is either the string to define it to or None to | ||
| define it without a particular value (equivalent of "#define | ||
| FOO" in source or -DFOO on Unix C compiler command line) | ||
| undef_macros : [string] | ||
| list of macros to undefine explicitly | ||
| library_dirs : [string] | ||
| list of directories to search for C/C++ libraries at link time | ||
| libraries : [string] | ||
| list of library names (not filenames or paths) to link against | ||
| runtime_library_dirs : [string] | ||
| list of directories to search for C/C++ libraries at run time | ||
| (for shared extensions, this is when the extension is loaded) | ||
| extra_objects : [string] | ||
| list of extra files to link with (eg. object files not implied | ||
| by 'sources', static library that must be explicitly specified, | ||
| binary resource files, etc.) | ||
| extra_compile_args : [string] | ||
| any extra platform- and compiler-specific information to use | ||
| when compiling the source files in 'sources'. For platforms and | ||
| compilers where "command line" makes sense, this is typically a | ||
| list of command-line arguments, but for other platforms it could | ||
| be anything. | ||
| extra_link_args : [string] | ||
| any extra platform- and compiler-specific information to use | ||
| when linking object files together to create the extension (or | ||
| to create a new static Python interpreter). Similar | ||
| interpretation as for 'extra_compile_args'. | ||
| export_symbols : [string] | ||
| list of symbols to be exported from a shared extension. Not | ||
| used on all platforms, and not generally necessary for Python | ||
| extensions, which typically export exactly one symbol: "init" + | ||
| extension_name. | ||
| swig_opts : [string] | ||
| any extra options to pass to SWIG if a source file has the .i | ||
| extension. | ||
| depends : [string] | ||
| list of files that the extension depends on | ||
| language : string | ||
| extension language (i.e. "c", "c++", "objc"). Will be detected | ||
| from the source extensions if not provided. | ||
| optional : boolean | ||
| specifies that a build failure in the extension should not abort the | ||
| build process, but simply not install the failing extension. | ||
| define_macros: list[tuple[str, str | None]] = field(default_factory=list) | ||
| """ | ||
| list of macros to define; each macro is defined using a 2-tuple, | ||
| where 'value' is either the string to define it to or None to | ||
| define it without a particular value (equivalent of "#define | ||
| FOO" in source or -DFOO on Unix C compiler command line) | ||
| """ | ||
|
|
||
| undef_macros: list[str] = field(default_factory=list) | ||
| """list of macros to undefine explicitly""" | ||
|
|
||
| library_dirs: list[str] = field(default_factory=list) | ||
| """list of directories to search for C/C++ libraries at link time""" | ||
|
|
||
| libraries: list[str] = field(default_factory=list) | ||
| """list of library names (not filenames or paths) to link against""" | ||
|
|
||
| runtime_library_dirs: list[str] = field(default_factory=list) | ||
| """ | ||
| list of directories to search for C/C++ libraries at run time | ||
| (for shared extensions, this is when the extension is loaded) | ||
| """ | ||
|
|
||
| extra_objects: list[str] = field(default_factory=list) | ||
| """ | ||
| list of extra files to link with (eg. object files not implied | ||
| by 'sources', static library that must be explicitly specified, | ||
| binary resource files, etc.) | ||
| """ | ||
|
|
||
| # When adding arguments to this constructor, be sure to update | ||
| # setup_keywords in core.py. | ||
| def __init__( | ||
| self, | ||
| name: str, | ||
| sources: Iterable[str | os.PathLike[str]], | ||
| include_dirs: list[str] | None = None, | ||
| define_macros: list[tuple[str, str | None]] | None = None, | ||
| undef_macros: list[str] | None = None, | ||
| library_dirs: list[str] | None = None, | ||
| libraries: list[str] | None = None, | ||
| runtime_library_dirs: list[str] | None = None, | ||
| extra_objects: list[str] | None = None, | ||
| extra_compile_args: list[str] | None = None, | ||
| extra_link_args: list[str] | None = None, | ||
| export_symbols: list[str] | None = None, | ||
| swig_opts: list[str] | None = None, | ||
| depends: list[str] | None = None, | ||
| language: str | None = None, | ||
| optional: bool | None = None, | ||
| **kw, # To catch unknown keywords | ||
| ): | ||
| if not isinstance(name, str): | ||
| extra_compile_args: list[str] = field(default_factory=list) | ||
| """ | ||
| any extra platform- and compiler-specific information to use | ||
| when compiling the source files in 'sources'. For platforms and | ||
| compilers where "command line" makes sense, this is typically a | ||
| list of command-line arguments, but for other platforms it could | ||
| be anything. | ||
| """ | ||
|
|
||
| extra_link_args: list[str] = field(default_factory=list) | ||
| """ | ||
| any extra platform- and compiler-specific information to use | ||
| when linking object files together to create the extension (or | ||
| to create a new static Python interpreter). Similar | ||
| interpretation as for 'extra_compile_args'. | ||
| """ | ||
|
|
||
| export_symbols: list[str] = field(default_factory=list) | ||
| """ | ||
| list of symbols to be exported from a shared extension. Not | ||
| used on all platforms, and not generally necessary for Python | ||
| extensions, which typically export exactly one symbol: "init" + | ||
| extension_name. | ||
| """ | ||
|
|
||
| swig_opts: list[str] = field(default_factory=list) | ||
| """ | ||
| any extra options to pass to SWIG if a source file has the .i | ||
| extension. | ||
| """ | ||
|
|
||
| depends: list[str] = field(default_factory=list) | ||
| """list of files that the extension depends on""" | ||
|
|
||
| language: str | None = None | ||
| """ | ||
| extension language (i.e. "c", "c++", "objc"). Will be detected | ||
| from the source extensions if not provided. | ||
| """ | ||
|
|
||
| optional: bool = False | ||
| """ | ||
| specifies that a build failure in the extension should not abort the | ||
| build process, but simply not install the failing extension. | ||
| """ | ||
|
|
||
| def __post_init__(self): | ||
| if not isinstance(self.name, str): | ||
| raise TypeError("'name' must be a string") | ||
|
|
||
| # handle the string case first; since strings are iterable, disallow them | ||
| if isinstance(sources, str): | ||
| if isinstance(self.sources, str): | ||
| raise TypeError( | ||
| "'sources' must be an iterable of strings or PathLike objects, not a string" | ||
| ) | ||
|
|
||
| # now we check if it's iterable and contains valid types | ||
| try: | ||
| self.sources = list(map(os.fspath, sources)) | ||
| self.sources = list(map(os.fspath, self.sources)) | ||
| except TypeError: | ||
| raise TypeError( | ||
| "'sources' must be an iterable of strings or PathLike objects" | ||
| ) | ||
|
|
||
| self.name = name | ||
| self.include_dirs = include_dirs or [] | ||
| self.define_macros = define_macros or [] | ||
| self.undef_macros = undef_macros or [] | ||
| self.library_dirs = library_dirs or [] | ||
| self.libraries = libraries or [] | ||
| self.runtime_library_dirs = runtime_library_dirs or [] | ||
| self.extra_objects = extra_objects or [] | ||
| self.extra_compile_args = extra_compile_args or [] | ||
| self.extra_link_args = extra_link_args or [] | ||
| self.export_symbols = export_symbols or [] | ||
| self.swig_opts = swig_opts or [] | ||
| self.depends = depends or [] | ||
| self.language = language | ||
| self.optional = optional | ||
|
|
||
| # If there are unknown keyword options, warn about them | ||
| if len(kw) > 0: | ||
| options = [repr(option) for option in kw] | ||
| options = ', '.join(sorted(options)) | ||
| msg = f"Unknown Extension options: {options}" | ||
| warnings.warn(msg) | ||
|
|
||
| def __repr__(self): | ||
| return f'<{self.__class__.__module__}.{self.__class__.__qualname__}({self.name!r}) at {id(self):#x}>' | ||
|
|
||
| # Legal keyword arguments for the Extension constructor | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The phrase "Legal keyword arguments for the Extension constructor" was copied from |
||
| _safe = tuple(f.name for f in fields(Extension)) | ||
|
|
||
|
|
||
| def read_setup_file(filename): # noqa: C901 | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.