Skip to content

Commit 1b1dcd7

Browse files
committed
Add pathlib types to _INVALID_BUILTIN_CLASSES
1 parent 019a666 commit 1b1dcd7

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Bugs fixed
9292
methods and attributes.
9393
Patch by Bénédikt Tran.
9494
* #12975: Avoid rendering a trailing comma in C and C++ multi-line signatures.
95+
* #13178: autodoc: Fix resolution for ``pathlib`` types.
96+
Patch by Adam Turner.
9597

9698
Testing
9799
-------

sphinx/util/typing.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
from __future__ import annotations
44

5+
import contextvars
56
import dataclasses
7+
import pathlib
8+
import struct
69
import sys
710
import types
811
import typing
912
from collections.abc import Callable, Sequence
10-
from contextvars import Context, ContextVar, Token
11-
from struct import Struct
1213
from typing import TYPE_CHECKING
1314

1415
from docutils import nodes
@@ -40,10 +41,19 @@
4041

4142
# classes that have an incorrect .__module__ attribute
4243
_INVALID_BUILTIN_CLASSES: Final[Mapping[object, str]] = {
43-
Context: 'contextvars.Context', # Context.__module__ == '_contextvars'
44-
ContextVar: 'contextvars.ContextVar', # ContextVar.__module__ == '_contextvars'
45-
Token: 'contextvars.Token', # Token.__module__ == '_contextvars'
46-
Struct: 'struct.Struct', # Struct.__module__ == '_struct'
44+
# types in 'contextvars' with <type>.__module__ == '_contextvars':
45+
contextvars.Context: 'contextvars.Context',
46+
contextvars.ContextVar: 'contextvars.ContextVar',
47+
contextvars.Token: 'contextvars.Token',
48+
# types in 'pathlib' with <type>.__module__ == 'pathlib._local':
49+
pathlib.Path: 'pathlib.Path',
50+
pathlib.PosixPath: 'pathlib.PosixPath',
51+
pathlib.PurePath: 'pathlib.PurePath',
52+
pathlib.PurePosixPath: 'pathlib.PurePosixPath',
53+
pathlib.PureWindowsPath: 'pathlib.PureWindowsPath',
54+
pathlib.WindowsPath: 'pathlib.WindowsPath',
55+
# types in 'struct' with <type>.__module__ == '_struct':
56+
struct.Struct: 'struct.Struct',
4757
# types in 'types' with <type>.__module__ == 'builtins':
4858
types.AsyncGeneratorType: 'types.AsyncGeneratorType',
4959
types.BuiltinFunctionType: 'types.BuiltinFunctionType',

tests/test_util/test_util_typing.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
from contextvars import Context, ContextVar, Token
1010
from enum import Enum
1111
from numbers import Integral
12+
from pathlib import (
13+
Path,
14+
PosixPath,
15+
PurePath,
16+
PurePosixPath,
17+
PureWindowsPath,
18+
WindowsPath,
19+
)
1220
from struct import Struct
1321
from types import (
1422
AsyncGeneratorType,
@@ -99,6 +107,9 @@ def test_restify():
99107
assert restify(TracebackType) == ':py:class:`types.TracebackType`'
100108
assert restify(TracebackType, 'smart') == ':py:class:`~types.TracebackType`'
101109

110+
assert restify(Path) == ':py:class:`pathlib.Path`'
111+
assert restify(Path, 'smart') == ':py:class:`~pathlib.Path`'
112+
102113
assert restify(Any) == ':py:obj:`~typing.Any`'
103114
assert restify(Any, 'smart') == ':py:obj:`~typing.Any`'
104115

@@ -111,10 +122,20 @@ def test_is_invalid_builtin_class():
111122
# of one of these classes has changed, and _INVALID_BUILTIN_CLASSES
112123
# in sphinx.util.typing needs to be updated.
113124
assert _INVALID_BUILTIN_CLASSES.keys() == {
125+
# contextvars
114126
Context,
115127
ContextVar,
116128
Token,
129+
# pathlib
130+
Path,
131+
PosixPath,
132+
PurePath,
133+
PurePosixPath,
134+
PureWindowsPath,
135+
WindowsPath,
136+
# struct
117137
Struct,
138+
# types
118139
AsyncGeneratorType,
119140
BuiltinFunctionType,
120141
BuiltinMethodType,
@@ -487,6 +508,10 @@ def test_stringify_annotation():
487508
assert ann_str == 'types.TracebackType'
488509
assert stringify_annotation(TracebackType, 'smart') == '~types.TracebackType'
489510

511+
ann_str = stringify_annotation(Path, 'fully-qualified-except-typing')
512+
assert ann_str == 'pathlib.Path'
513+
assert stringify_annotation(Path, 'smart') == '~pathlib.Path'
514+
490515
assert stringify_annotation(Any, 'fully-qualified-except-typing') == 'Any'
491516
assert stringify_annotation(Any, 'fully-qualified') == 'typing.Any'
492517
assert stringify_annotation(Any, 'smart') == '~typing.Any'

0 commit comments

Comments
 (0)