Skip to content

Commit 3d853d5

Browse files
Improve path-related type hints for setuptools.Extension() and distutils.CCompiler() (#12958)
Co-authored-by: Avasam <[email protected]>
1 parent 1ae7e61 commit 3d853d5

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

stdlib/distutils/ccompiler.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from _typeshed import BytesPath, StrPath, Unused
2-
from collections.abc import Callable, Iterable
2+
from collections.abc import Callable, Iterable, Sequence
33
from distutils.file_util import _BytesPathT, _StrPathT
44
from typing import Literal, overload
55
from typing_extensions import TypeAlias, TypeVarTuple, Unpack
@@ -63,7 +63,7 @@ class CCompiler:
6363
def set_executables(self, **args: str) -> None: ...
6464
def compile(
6565
self,
66-
sources: list[str],
66+
sources: Sequence[StrPath],
6767
output_dir: str | None = None,
6868
macros: list[_Macro] | None = None,
6969
include_dirs: list[str] | None = None,
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
from __future__ import annotations
2+
13
import distutils.command.sdist
4+
from _typeshed import StrPath
5+
from os import PathLike
6+
from pathlib import Path
7+
8+
from setuptools._distutils.ccompiler import CCompiler
29

310
c = distutils.command.sdist.sdist
11+
12+
# Test CCompiler().compile with varied sources
13+
14+
compiler = CCompiler()
15+
16+
str_list: list[str] = ["file1.c", "file2.c"]
17+
compiler.compile(sources=str_list)
18+
19+
path_list: list[Path] = [Path("file1.c"), Path("file2.c")]
20+
compiler.compile(sources=path_list)
21+
22+
pathlike_list: list[PathLike[str]] = [Path("file1.c"), Path("file2.c")]
23+
compiler.compile(sources=pathlike_list)
24+
25+
strpath_list: list[StrPath] = [Path("file1.c"), "file2.c"]
26+
compiler.compile(sources=strpath_list)
27+
28+
# Direct literals should also work
29+
compiler.compile(sources=["file1.c", "file2.c"])
30+
compiler.compile(sources=[Path("file1.c"), Path("file2.c")])
31+
compiler.compile(sources=[Path("file1.c"), "file2.c"])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from os import PathLike
4+
from pathlib import Path
5+
6+
from setuptools import Extension
7+
8+
# Dummy extensions
9+
ext1 = Extension(name="test1", sources=["file1.c", "file2.c"]) # plain list[str] works
10+
11+
path_sources: list[Path] = [Path("file1.c"), Path("file2.c")]
12+
ext2 = Extension(name="test2", sources=path_sources) # list of Path(s)
13+
14+
mixed_sources: list[str | PathLike[str]] = [Path("file1.c"), "file2.c"] # or list[StrPath]
15+
ext3 = Extension(name="test3", sources=mixed_sources) # mixed types

stubs/setuptools/setuptools/_distutils/ccompiler.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from _typeshed import BytesPath, StrPath, Unused
2-
from collections.abc import Callable, Iterable
2+
from collections.abc import Callable, Iterable, Sequence
33
from typing import ClassVar, Literal, TypeVar, overload
44
from typing_extensions import TypeAlias, TypeVarTuple, Unpack
55

@@ -67,7 +67,7 @@ class CCompiler:
6767
def set_executables(self, **args: str) -> None: ...
6868
def compile(
6969
self,
70-
sources: list[str],
70+
sources: Sequence[StrPath],
7171
output_dir: str | None = None,
7272
macros: list[_Macro] | None = None,
7373
include_dirs: list[str] | None = None,

stubs/setuptools/setuptools/_distutils/extension.pyi

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
from _typeshed import StrPath
2+
from os import PathLike
3+
from pathlib import Path
4+
15
class Extension:
26
name: str
3-
sources: list[str]
7+
sources: list[str] | list[StrPath]
48
include_dirs: list[str]
59
define_macros: list[tuple[str, str | None]]
610
undef_macros: list[str]
@@ -18,7 +22,7 @@ class Extension:
1822
def __init__(
1923
self,
2024
name: str,
21-
sources: list[str],
25+
sources: list[str] | list[PathLike[str]] | list[Path] | list[StrPath],
2226
include_dirs: list[str] | None = None,
2327
define_macros: list[tuple[str, str | None]] | None = None,
2428
undef_macros: list[str] | None = None,

stubs/setuptools/setuptools/extension.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from _typeshed import StrPath
2+
from os import PathLike
3+
from pathlib import Path
24

35
from ._distutils.extension import Extension as _Extension
46

@@ -9,7 +11,7 @@ class Extension(_Extension):
911
def __init__(
1012
self,
1113
name: str,
12-
sources: list[StrPath],
14+
sources: list[str] | list[PathLike[str]] | list[Path] | list[StrPath],
1315
include_dirs: list[str] | None = None,
1416
define_macros: list[tuple[str, str | None]] | None = None,
1517
undef_macros: list[str] | None = None,

0 commit comments

Comments
 (0)