Skip to content

Commit

Permalink
Add further types to _INVALID_BUILTIN_CLASSES
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 23, 2025
1 parent ea5fc59 commit 30783ce
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
37 changes: 37 additions & 0 deletions sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
from __future__ import annotations

import contextvars
import ctypes
import dataclasses
import io
import json
import lzma
import multiprocessing
import pathlib
import pickle # NoQA: S403
import struct
import sys
import types
import typing
import weakref
import zipfile
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -45,13 +53,37 @@
contextvars.Context: 'contextvars.Context',
contextvars.ContextVar: 'contextvars.ContextVar',
contextvars.Token: 'contextvars.Token',
# types in 'ctypes' with <type>.__module__ == '_ctypes':
ctypes.Array: 'ctypes.Array',
ctypes.Structure: 'ctypes.Structure',
ctypes.Union: 'ctypes.Union',
# types in 'io' with <type>.__module__ == '_io':
io.FileIO: 'io.FileIO',
io.BytesIO: 'io.BytesIO',
io.StringIO: 'io.StringIO',
io.BufferedReader: 'io.BufferedReader',
io.BufferedWriter: 'io.BufferedWriter',
io.BufferedRWPair: 'io.BufferedRWPair',
io.BufferedRandom: 'io.BufferedRandom',
io.TextIOWrapper: 'io.TextIOWrapper',
# types in 'json' with <type>.__module__ == 'json.{decoder,encoder}':
json.JSONDecoder: 'json.JSONDecoder',
json.JSONEncoder: 'json.JSONEncoder',
# types in 'lzma' with <type>.__module__ == '_lzma':
lzma.LZMACompressor: 'lzma.LZMACompressor',
lzma.LZMADecompressor: 'lzma.LZMADecompressor',
# types in 'multiprocessing' with <type>.__module__ == 'multiprocessing.context':
multiprocessing.Process: 'multiprocessing.Process',
# types in 'pathlib' with <type>.__module__ == 'pathlib._local':
pathlib.Path: 'pathlib.Path',
pathlib.PosixPath: 'pathlib.PosixPath',
pathlib.PurePath: 'pathlib.PurePath',
pathlib.PurePosixPath: 'pathlib.PurePosixPath',
pathlib.PureWindowsPath: 'pathlib.PureWindowsPath',
pathlib.WindowsPath: 'pathlib.WindowsPath',
# types in 'pickle' with <type>.__module__ == 'pickle':
pickle.Pickler: 'pickle.Pickler',
pickle.Unpickler: 'pickle.Unpickler',
# types in 'struct' with <type>.__module__ == '_struct':
struct.Struct: 'struct.Struct',
# types in 'types' with <type>.__module__ == 'builtins':
Expand All @@ -75,6 +107,11 @@
types.ModuleType: 'types.ModuleType',
types.TracebackType: 'types.TracebackType',
types.WrapperDescriptorType: 'types.WrapperDescriptorType',
# types in 'weakref' with <type>.__module__ == '_weakrefset':
weakref.WeakSet: 'weakref.WeakSet',
# types in 'zipfile' with <type>.__module__ == 'zipfile._path':
zipfile.Path: 'zipfile.Path',
zipfile.CompleteDirs: 'zipfile.CompleteDirs',
}


Expand Down
75 changes: 75 additions & 0 deletions tests/test_util/test_util_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

from __future__ import annotations

import ctypes
import dataclasses
import sys
import typing as t
import zipfile
from collections import abc
from contextvars import Context, ContextVar, Token
from enum import Enum
from io import (
BufferedRandom,
BufferedReader,
BufferedRWPair,
BufferedWriter,
BytesIO,
FileIO,
StringIO,
TextIOWrapper,
)
from json import JSONDecoder, JSONEncoder
from lzma import LZMACompressor, LZMADecompressor
from multiprocessing import Process
from numbers import Integral
from pathlib import (
Path,
Expand All @@ -17,6 +32,7 @@
PureWindowsPath,
WindowsPath,
)
from pickle import Pickler, Unpickler
from struct import Struct
from types import (
AsyncGeneratorType,
Expand Down Expand Up @@ -54,6 +70,7 @@
TypeVar,
Union,
)
from weakref import WeakSet

from sphinx.ext.autodoc import mock
from sphinx.util.typing import _INVALID_BUILTIN_CLASSES, restify, stringify_annotation
Expand Down Expand Up @@ -126,13 +143,37 @@ def test_is_invalid_builtin_class():
Context,
ContextVar,
Token,
# ctypes
ctypes.Array,
ctypes.Structure,
ctypes.Union,
# io
FileIO,
BytesIO,
StringIO,
BufferedReader,
BufferedWriter,
BufferedRWPair,
BufferedRandom,
TextIOWrapper,
# json
JSONDecoder,
JSONEncoder,
# lzma
LZMACompressor,
LZMADecompressor,
# multiprocessing
Process,
# pathlib
Path,
PosixPath,
PurePath,
PurePosixPath,
PureWindowsPath,
WindowsPath,
# pickle
Pickler,
Unpickler,
# struct
Struct,
# types
Expand All @@ -156,11 +197,37 @@ def test_is_invalid_builtin_class():
ModuleType,
TracebackType,
WrapperDescriptorType,
# weakref
WeakSet,
# zipfile
zipfile.Path,
zipfile.CompleteDirs,
}
# contextvars
assert Context.__module__ == '_contextvars'
assert ContextVar.__module__ == '_contextvars'
assert Token.__module__ == '_contextvars'
# ctypes
assert ctypes.Array.__module__ == '_ctypes'
assert ctypes.Structure.__module__ == '_ctypes'
assert ctypes.Union.__module__ == '_ctypes'
# io
assert FileIO.__module__ == '_io'
assert BytesIO.__module__ == '_io'
assert StringIO.__module__ == '_io'
assert BufferedReader.__module__ == '_io'
assert BufferedWriter.__module__ == '_io'
assert BufferedRWPair.__module__ == '_io'
assert BufferedRandom.__module__ == '_io'
assert TextIOWrapper.__module__ == '_io'
# json
assert JSONDecoder.__module__ == 'json.decoder'
assert JSONEncoder.__module__ == 'json.encoder'
# lzma
assert LZMACompressor.__module__ == '_lzma'
assert LZMADecompressor.__module__ == '_lzma'
# multiprocessing
assert Process.__module__ == '_io'
if sys.version_info[:2] >= (3, 13):
# pathlib
assert Path.__module__ == 'pathlib._local'
Expand All @@ -169,6 +236,9 @@ def test_is_invalid_builtin_class():
assert PurePosixPath.__module__ == 'pathlib._local'
assert PureWindowsPath.__module__ == 'pathlib._local'
assert WindowsPath.__module__ == 'pathlib._local'
# pickle
assert Pickler.__module__ == '_pickle'
assert Unpickler.__module__ == '_pickle'
# struct
assert Struct.__module__ == '_struct'
# types
Expand All @@ -192,6 +262,11 @@ def test_is_invalid_builtin_class():
assert ModuleType.__module__ == 'builtins'
assert TracebackType.__module__ == 'builtins'
assert WrapperDescriptorType.__module__ == 'builtins'
# weakref
assert WeakSet.__module__ == '_weakrefset'
# zipfile
assert zipfile.Path.__module__ == 'zipfile._path'
assert zipfile.CompleteDirs.__module__ == 'zipfile._path'


def test_restify_type_hints_containers():
Expand Down

0 comments on commit 30783ce

Please sign in to comment.