Skip to content

Commit 88ec0d1

Browse files
committed
config,mark,monkeypatch: use common NOTSET from compat
1 parent 9b976ee commit 88ec0d1

File tree

4 files changed

+24
-44
lines changed

4 files changed

+24
-44
lines changed

src/_pytest/config/__init__.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from _pytest._code.code import TracebackStyle
5656
from _pytest._io import TerminalWriter
5757
from _pytest.compat import assert_never
58+
from _pytest.compat import NOTSET
5859
from _pytest.config.argparsing import Argument
5960
from _pytest.config.argparsing import FILE_OR_DIR
6061
from _pytest.config.argparsing import Parser
@@ -907,14 +908,6 @@ def _get_plugin_specs_as_list(
907908
)
908909

909910

910-
class Notset:
911-
def __repr__(self):
912-
return "<NOTSET>"
913-
914-
915-
notset = Notset()
916-
917-
918911
def _iter_rewritable_modules(package_files: Iterable[str]) -> Iterator[str]:
919912
"""Given an iterable of file names in a source distribution, return the "names" that should
920913
be marked for assertion rewrite.
@@ -1840,7 +1833,7 @@ def _getconftest_pathlist(
18401833
values.append(relroot)
18411834
return values
18421835

1843-
def getoption(self, name: str, default: Any = notset, skip: bool = False):
1836+
def getoption(self, name: str, default: Any = NOTSET, skip: bool = False):
18441837
"""Return command line option value.
18451838
18461839
:param name: Name of the option. You may also specify
@@ -1857,7 +1850,7 @@ def getoption(self, name: str, default: Any = notset, skip: bool = False):
18571850
raise AttributeError(name)
18581851
return val
18591852
except AttributeError as e:
1860-
if default is not notset:
1853+
if default is not NOTSET:
18611854
return default
18621855
if skip:
18631856
import pytest

src/_pytest/config/argparsing.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515

1616
from .exceptions import UsageError
1717
import _pytest._io
18+
from _pytest.compat import NOTSET
1819
from _pytest.deprecated import check_ispytest
1920

2021

2122
FILE_OR_DIR = "file_or_dir"
2223

2324

24-
class NotSet:
25-
def __repr__(self) -> str:
26-
return "<notset>"
27-
28-
29-
NOT_SET = NotSet()
30-
31-
3225
@final
3326
class Parser:
3427
"""Parser for command line arguments and config-file values.
@@ -191,7 +184,7 @@ def addini(
191184
"string", "paths", "pathlist", "args", "linelist", "bool", "int", "float"
192185
]
193186
| None = None,
194-
default: Any = NOT_SET,
187+
default: Any = NOTSET,
195188
*,
196189
aliases: Sequence[str] = (),
197190
) -> None:
@@ -251,7 +244,7 @@ def addini(
251244
)
252245
if type is None:
253246
type = "string"
254-
if default is NOT_SET:
247+
if default is NOTSET:
255248
default = get_ini_default_for_type(type)
256249

257250
self._inidict[name] = (help, type, default)

src/_pytest/mark/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
from .structures import MarkDecorator
2020
from .structures import MarkGenerator
2121
from .structures import ParameterSet
22+
from _pytest.compat import NOTSET
2223
from _pytest.config import Config
2324
from _pytest.config import ExitCode
2425
from _pytest.config import hookimpl
2526
from _pytest.config import UsageError
26-
from _pytest.config.argparsing import NOT_SET
2727
from _pytest.config.argparsing import Parser
2828
from _pytest.stash import StashKey
2929

@@ -247,7 +247,7 @@ def __call__(self, name: str, /, **kwargs: str | int | bool | None) -> bool:
247247
return False
248248

249249
for mark in matches: # pylint: disable=consider-using-any-or-all
250-
if all(mark.kwargs.get(k, NOT_SET) == v for k, v in kwargs.items()):
250+
if all(mark.kwargs.get(k, NOTSET) == v for k, v in kwargs.items()):
251251
return True
252252
return False
253253

src/_pytest/monkeypatch.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from typing import TypeVar
1818
import warnings
1919

20+
from _pytest.compat import NOTSET
21+
from _pytest.compat import NotSetType
2022
from _pytest.deprecated import MONKEYPATCH_LEGACY_NAMESPACE_PACKAGES
2123
from _pytest.fixtures import fixture
2224
from _pytest.warning_types import PytestWarning
@@ -107,14 +109,6 @@ def derive_importpath(import_path: str, raising: bool) -> tuple[str, object]:
107109
return attr, target
108110

109111

110-
class Notset:
111-
def __repr__(self) -> str:
112-
return "<notset>"
113-
114-
115-
notset = Notset()
116-
117-
118112
@final
119113
class MonkeyPatch:
120114
"""Helper to conveniently monkeypatch attributes/items/environment
@@ -167,7 +161,7 @@ def setattr(
167161
self,
168162
target: str,
169163
name: object,
170-
value: Notset = ...,
164+
value: NotSetType = ...,
171165
raising: bool = ...,
172166
) -> None: ...
173167

@@ -184,7 +178,7 @@ def setattr(
184178
self,
185179
target: str | object,
186180
name: object | str,
187-
value: object = notset,
181+
value: object = NOTSET,
188182
raising: bool = True,
189183
) -> None:
190184
"""
@@ -225,7 +219,7 @@ def setattr(
225219
__tracebackhide__ = True
226220
import inspect
227221

228-
if isinstance(value, Notset):
222+
if value is NOTSET:
229223
if not isinstance(target, str):
230224
raise TypeError(
231225
"use setattr(target, name, value) or "
@@ -242,20 +236,20 @@ def setattr(
242236
"import string"
243237
)
244238

245-
oldval = getattr(target, name, notset)
246-
if raising and oldval is notset:
239+
oldval = getattr(target, name, NOTSET)
240+
if raising and oldval is NOTSET:
247241
raise AttributeError(f"{target!r} has no attribute {name!r}")
248242

249243
# avoid class descriptors like staticmethod/classmethod
250244
if inspect.isclass(target):
251-
oldval = target.__dict__.get(name, notset)
245+
oldval = target.__dict__.get(name, NOTSET)
252246
self._setattr.append((target, name, oldval))
253247
setattr(target, name, value)
254248

255249
def delattr(
256250
self,
257251
target: object | str,
258-
name: str | Notset = notset,
252+
name: str | NotSetType = NOTSET,
259253
raising: bool = True,
260254
) -> None:
261255
"""Delete attribute ``name`` from ``target``.
@@ -270,7 +264,7 @@ def delattr(
270264
__tracebackhide__ = True
271265
import inspect
272266

273-
if isinstance(name, Notset):
267+
if name is NOTSET:
274268
if not isinstance(target, str):
275269
raise TypeError(
276270
"use delattr(target, name) or "
@@ -283,16 +277,16 @@ def delattr(
283277
if raising:
284278
raise AttributeError(name)
285279
else:
286-
oldval = getattr(target, name, notset)
280+
oldval = getattr(target, name, NOTSET)
287281
# Avoid class descriptors like staticmethod/classmethod.
288282
if inspect.isclass(target):
289-
oldval = target.__dict__.get(name, notset)
283+
oldval = target.__dict__.get(name, NOTSET)
290284
self._setattr.append((target, name, oldval))
291285
delattr(target, name)
292286

293287
def setitem(self, dic: Mapping[K, V], name: K, value: V) -> None:
294288
"""Set dictionary entry ``name`` to value."""
295-
self._setitem.append((dic, name, dic.get(name, notset)))
289+
self._setitem.append((dic, name, dic.get(name, NOTSET)))
296290
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
297291
dic[name] = value # type: ignore[index]
298292

@@ -306,7 +300,7 @@ def delitem(self, dic: Mapping[K, V], name: K, raising: bool = True) -> None:
306300
if raising:
307301
raise KeyError(name)
308302
else:
309-
self._setitem.append((dic, name, dic.get(name, notset)))
303+
self._setitem.append((dic, name, dic.get(name, NOTSET)))
310304
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
311305
del dic[name] # type: ignore[attr-defined]
312306

@@ -410,13 +404,13 @@ def undo(self) -> None:
410404
Prefer to use :meth:`context() <pytest.MonkeyPatch.context>` instead.
411405
"""
412406
for obj, name, value in reversed(self._setattr):
413-
if value is not notset:
407+
if value is not NOTSET:
414408
setattr(obj, name, value)
415409
else:
416410
delattr(obj, name)
417411
self._setattr[:] = []
418412
for dictionary, key, value in reversed(self._setitem):
419-
if value is notset:
413+
if value is NOTSET:
420414
try:
421415
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
422416
del dictionary[key] # type: ignore[attr-defined]

0 commit comments

Comments
 (0)