Skip to content
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

Improve path-related type hints for setuptools.Extension() and distutils.CCompiler() #12958

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stdlib/distutils/ccompiler.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from _typeshed import BytesPath, StrPath, Unused
from collections.abc import Callable, Iterable
from collections.abc import Callable, Iterable, Sequence
from distutils.file_util import _BytesPathT, _StrPathT
from typing import Literal, overload
from typing_extensions import TypeAlias, TypeVarTuple, Unpack
Expand Down Expand Up @@ -63,7 +63,7 @@ class CCompiler:
def set_executables(self, **args: str) -> None: ...
def compile(
self,
sources: list[str],
sources: Sequence[StrPath],
output_dir: str | None = None,
macros: list[_Macro] | None = None,
include_dirs: list[str] | None = None,
Expand Down
28 changes: 28 additions & 0 deletions stubs/setuptools/@tests/test_cases/check_distutils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
from __future__ import annotations

import distutils.command.sdist
from _typeshed import StrPath
from os import PathLike
from pathlib import Path

from setuptools._distutils.ccompiler import CCompiler

c = distutils.command.sdist.sdist

# Test CCompiler().compile with varied sources

compiler = CCompiler()

str_list: list[str] = ["file1.c", "file2.c"]
compiler.compile(sources=str_list)

path_list: list[Path] = [Path("file1.c"), Path("file2.c")]
compiler.compile(sources=path_list)

pathlike_list: list[PathLike[str]] = [Path("file1.c"), Path("file2.c")]
compiler.compile(sources=pathlike_list)

strpath_list: list[StrPath] = [Path("file1.c"), "file2.c"]
compiler.compile(sources=strpath_list)

# Direct literals should also work
compiler.compile(sources=["file1.c", "file2.c"])
compiler.compile(sources=[Path("file1.c"), Path("file2.c")])
compiler.compile(sources=[Path("file1.c"), "file2.c"])
15 changes: 15 additions & 0 deletions stubs/setuptools/@tests/test_cases/check_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from os import PathLike
from pathlib import Path

from setuptools import Extension

# Dummy extensions
ext1 = Extension(name="test1", sources=["file1.c", "file2.c"]) # plain list[str] works

path_sources: list[Path] = [Path("file1.c"), Path("file2.c")]
ext2 = Extension(name="test2", sources=path_sources) # list of Path(s)

mixed_sources: list[str | PathLike[str]] = [Path("file1.c"), "file2.c"] # or list[StrPath]
ext3 = Extension(name="test3", sources=mixed_sources) # mixed types
4 changes: 2 additions & 2 deletions stubs/setuptools/setuptools/_distutils/ccompiler.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from _typeshed import BytesPath, StrPath, Unused
from collections.abc import Callable, Iterable
from collections.abc import Callable, Iterable, Sequence
from typing import ClassVar, Literal, TypeVar, overload
from typing_extensions import TypeAlias, TypeVarTuple, Unpack

Expand Down Expand Up @@ -67,7 +67,7 @@ class CCompiler:
def set_executables(self, **args: str) -> None: ...
def compile(
self,
sources: list[str],
sources: Sequence[StrPath],
output_dir: str | None = None,
macros: list[_Macro] | None = None,
include_dirs: list[str] | None = None,
Expand Down
8 changes: 6 additions & 2 deletions stubs/setuptools/setuptools/_distutils/extension.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from _typeshed import StrPath
from os import PathLike
from pathlib import Path

class Extension:
name: str
sources: list[str]
sources: list[str] | list[StrPath]
include_dirs: list[str]
define_macros: list[tuple[str, str | None]]
undef_macros: list[str]
Expand All @@ -18,7 +22,7 @@ class Extension:
def __init__(
self,
name: str,
sources: list[str],
sources: list[str] | list[PathLike[str]] | list[Path] | list[StrPath],
include_dirs: list[str] | None = None,
define_macros: list[tuple[str, str | None]] | None = None,
undef_macros: list[str] | None = None,
Expand Down
4 changes: 3 additions & 1 deletion stubs/setuptools/setuptools/extension.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from _typeshed import StrPath
from os import PathLike
from pathlib import Path

from ._distutils.extension import Extension as _Extension

Expand All @@ -9,7 +11,7 @@ class Extension(_Extension):
def __init__(
self,
name: str,
sources: list[StrPath],
sources: list[str] | list[PathLike[str]] | list[Path] | list[StrPath],
include_dirs: list[str] | None = None,
define_macros: list[tuple[str, str | None]] | None = None,
undef_macros: list[str] | None = None,
Expand Down