Skip to content

Commit 7046da1

Browse files
committed
Make everywhere use the newer attrs APIs.
1 parent e30b48e commit 7046da1

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

jsonschema/_types.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from typing import Any, Callable, Mapping
44
import numbers
55

6+
from attrs import evolve, field, frozen
67
from rpds import HashTrieMap
7-
import attr
88

99
from jsonschema.exceptions import UndefinedTypeCheck
1010

1111

12-
# unfortunately, the type of HashTrieMap is generic, and if used as the attr.ib
12+
# unfortunately, the type of HashTrieMap is generic, and if used as an attrs
1313
# converter, the generic type is presented to mypy, which then fails to match
1414
# the concrete type of a type checker mapping
1515
# this "do nothing" wrapper presents the correct information to mypy
@@ -57,7 +57,7 @@ def is_any(checker, instance):
5757
return True
5858

5959

60-
@attr.s(frozen=True, repr=False)
60+
@frozen(repr=False)
6161
class TypeChecker:
6262
"""
6363
A :kw:`type` property checker.
@@ -80,10 +80,7 @@ class TypeChecker:
8080

8181
_type_checkers: HashTrieMap[
8282
str, Callable[[TypeChecker, Any], bool],
83-
] = attr.ib(
84-
default=HashTrieMap(),
85-
converter=_typed_map_converter,
86-
)
83+
] = field(default=HashTrieMap(), converter=_typed_map_converter)
8784

8885
def __repr__(self):
8986
types = ", ".join(repr(k) for k in sorted(self._type_checkers))
@@ -146,7 +143,7 @@ def redefine_many(self, definitions=()) -> TypeChecker:
146143
A dictionary mapping types to their checking functions.
147144
"""
148145
type_checkers = self._type_checkers.update(definitions)
149-
return attr.evolve(self, type_checkers=type_checkers)
146+
return evolve(self, type_checkers=type_checkers)
150147

151148
def remove(self, *types) -> TypeChecker:
152149
"""
@@ -170,7 +167,7 @@ def remove(self, *types) -> TypeChecker:
170167
type_checkers = type_checkers.remove(each)
171168
except KeyError:
172169
raise UndefinedTypeCheck(each)
173-
return attr.evolve(self, type_checkers=type_checkers)
170+
return evolve(self, type_checkers=type_checkers)
174171

175172

176173
draft3_type_checker = TypeChecker(

jsonschema/cli.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
except ImportError:
1717
from pkgutil_resolve_name import resolve_name # type: ignore
1818

19-
import attr
19+
from attrs import define, field
2020

2121
from jsonschema.exceptions import SchemaError
2222
from jsonschema.validators import _RefResolver, validator_for
@@ -36,12 +36,12 @@ class _CannotLoadFile(Exception):
3636
pass
3737

3838

39-
@attr.s
39+
@define
4040
class _Outputter:
4141

42-
_formatter = attr.ib()
43-
_stdout = attr.ib()
44-
_stderr = attr.ib()
42+
_formatter = field()
43+
_stdout = field()
44+
_stderr = field()
4545

4646
@classmethod
4747
def from_arguments(cls, arguments, stdout, stderr):
@@ -78,7 +78,7 @@ def validation_success(self, **kwargs):
7878
self._stdout.write(self._formatter.validation_success(**kwargs))
7979

8080

81-
@attr.s
81+
@define
8282
class _PrettyFormatter:
8383

8484
_ERROR_MSG = dedent(
@@ -120,10 +120,10 @@ def validation_success(self, instance_path):
120120
return self._SUCCESS_MSG.format(path=instance_path)
121121

122122

123-
@attr.s
123+
@define
124124
class _PlainFormatter:
125125

126-
_error_format = attr.ib()
126+
_error_format = field()
127127

128128
def filenotfound_error(self, path, exc_info):
129129
return "{!r} does not exist.\n".format(path)

jsonschema/exceptions.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import itertools
1212
import warnings
1313

14+
from attrs import define
1415
from referencing.exceptions import Unresolvable as _Unresolvable
15-
import attr
1616

1717
from jsonschema import _utils
1818

@@ -193,7 +193,7 @@ class SchemaError(_Error):
193193
_word_for_instance_in_error_message = "schema"
194194

195195

196-
@attr.s(hash=True)
196+
@define(slots=False)
197197
class _RefResolutionError(Exception):
198198
"""
199199
A ref could not be resolved.
@@ -205,7 +205,12 @@ class _RefResolutionError(Exception):
205205
"directly catch referencing.exceptions.Unresolvable."
206206
)
207207

208-
_cause = attr.ib()
208+
_cause: Exception
209+
210+
def __eq__(self, other):
211+
if self.__class__ is not other.__class__:
212+
return NotImplemented
213+
return self._cause == other._cause
209214

210215
def __str__(self):
211216
return str(self._cause)

jsonschema/tests/test_validators.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from contextlib import contextmanager
55
from decimal import Decimal
66
from io import BytesIO
7+
from typing import Any
78
from unittest import TestCase, mock
89
from urllib.request import pathname2url
910
import json
@@ -12,8 +13,8 @@
1213
import tempfile
1314
import warnings
1415

16+
from attrs import define, field
1517
from referencing.jsonschema import DRAFT202012
16-
import attr
1718
import referencing.exceptions
1819

1920
from jsonschema import (
@@ -1570,10 +1571,10 @@ def test_evolve_with_subclass(self):
15701571
"""
15711572

15721573
with self.assertWarns(DeprecationWarning):
1573-
@attr.s
1574+
@define
15741575
class OhNo(self.Validator):
1575-
foo = attr.ib(factory=lambda: [1, 2, 3])
1576-
_bar = attr.ib(default=37)
1576+
foo = field(factory=lambda: [1, 2, 3])
1577+
_bar = field(default=37)
15771578

15781579
validator = OhNo({}, bar=12)
15791580
self.assertEqual(validator.foo, [1, 2, 3])
@@ -2382,10 +2383,10 @@ def key(error):
23822383
return sorted(errors, key=key)
23832384

23842385

2385-
@attr.s
2386+
@define
23862387
class ReallyFakeRequests:
23872388

2388-
_responses = attr.ib()
2389+
_responses: dict[str, Any]
23892390

23902391
def get(self, url):
23912392
response = self._responses.get(url)
@@ -2394,10 +2395,10 @@ def get(self, url):
23942395
return _ReallyFakeJSONResponse(json.dumps(response))
23952396

23962397

2397-
@attr.s
2398+
@define
23982399
class _ReallyFakeJSONResponse:
23992400

2400-
_response = attr.ib()
2401+
_response: str
24012402

24022403
def json(self):
24032404
return json.loads(self._response)

0 commit comments

Comments
 (0)