Skip to content

GH-130798: Add type hints to pathlib.types #131639

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
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
matrix:
target: [
"Lib/_pyrepl",
"Lib/pathlib",
"Lib/test/libregrtest",
"Tools/build",
"Tools/cases_generator",
Expand Down
13 changes: 10 additions & 3 deletions Lib/pathlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
import posixpath
import sys
from errno import *
from glob import _StringGlobber, _no_recurse_symlinks
from glob import _StringGlobber, _no_recurse_symlinks # type: ignore[attr-defined]
from itertools import chain
from stat import S_ISDIR, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
from _collections_abc import Sequence

# types
if False:
from types import ModuleType

pwd: ModuleType | None
grp: ModuleType | None

try:
import pwd
except ImportError:
Expand All @@ -26,7 +33,7 @@
except ImportError:
grp = None

from pathlib._os import (
from ._os import (
PathInfo, DirEntryInfo,
ensure_different_files, ensure_distinct_paths,
copyfile2, copyfileobj, magic_open, copy_info,
Expand All @@ -46,7 +53,7 @@ class UnsupportedOperation(NotImplementedError):
pass


class _PathParents(Sequence):
class _PathParents(Sequence["PurePath"]):
"""This object provides sequence-like access to the logical ancestors
of a path. Don't try to construct it yourself."""
__slots__ = ('_path', '_drv', '_root', '_tail')
Expand Down
17 changes: 17 additions & 0 deletions Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
import io
import os
import sys

# typing
if False:
from collections.abc import Callable
from types import ModuleType

type CopyFunc = Callable[[int, int], None]

fcntl: ModuleType | None
posix: ModuleType | None
_winapi: ModuleType | None
_ficlone: CopyFunc | None
_fcopyfile: CopyFunc | None
_copy_file_range: CopyFunc | None
_sendfile: CopyFunc | None
copyfile2: CopyFunc | None

try:
import fcntl
except ImportError:
Expand Down
25 changes: 25 additions & 0 deletions Lib/pathlib/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Config file for running mypy on pathlib.
# Run mypy by invoking `mypy --config-file Lib/pathlib/mypy.ini`
# on the command-line from the repo root

[mypy]
files = Lib/pathlib
mypy_path = $MYPY_CONFIG_FILE_DIR/../../Misc/mypy
explicit_package_bases = True
python_version = 3.13
platform = linux
pretty = True

# ... Enable most stricter settings
enable_error_code = ignore-without-code,redundant-expr
strict = True

# can't enable before glob isn't typed ...
warn_return_any = False
disable_error_code = attr-defined

# Various stricter settings that we can't yet enable
# Try to enable these in the following order:
disallow_untyped_calls = False
disallow_untyped_defs = False
check_untyped_defs = False
Loading
Loading