Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 0429579

Browse files
wbolsterlovelydinosaur
authored andcommitted
Use Python syntax for type hints instead of comments (#50)
This changes all type hints to use modern Python (3.6+) syntax instead of special comments, but leaves the ‘type: ignore’ comments intact. Most of these changes are purely mechanical, except for these: - It seems the _message_dict can only ever contain str and int keys, so make that explicit instead of using `typing.Any`. - The call to `datetime.time(**kwargs, tzinfo=None)` gives mypy warnings which can be avoided by reordering the arguments. Changing it to `datetime.time(tzinfo=None, **kwargs)` will behave the same, since `kwargs` will never contain a `tzinfo` key, because `TIME_REGEX` does not have a capturing group with that name.
1 parent ce27434 commit 0429579

8 files changed

Lines changed: 21 additions & 23 deletions

File tree

typesystem/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ def __init__(
138138
assert len(messages)
139139

140140
self._messages = messages
141-
self._message_dict = (
142-
{}
143-
) # type: typing.Dict[typing.Any, typing.Union[str, dict]]
141+
self._message_dict: typing.Dict[
142+
typing.Union[int, str], typing.Union[str, dict]
143+
] = {}
144144

145145
# Populate 'self._message_dict'
146146
for message in messages:

typesystem/fields.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
class Field:
20-
errors = {} # type: typing.Dict[str, str]
20+
errors: typing.Dict[str, str] = {}
2121
_creation_counter = 0
2222

2323
def __init__(
@@ -189,7 +189,7 @@ def serialize(self, obj: typing.Any) -> typing.Any:
189189

190190

191191
class Number(Field):
192-
numeric_type = None # type: type
192+
numeric_type: typing.Optional[type] = None
193193
errors = {
194194
"type": "Must be a number.",
195195
"null": "May not be null.",
@@ -621,7 +621,7 @@ def validate(self, value: typing.Any, *, strict: bool = False) -> typing.Any:
621621

622622
# Ensure all items are of the right type.
623623
validated = []
624-
error_messages = [] # type: typing.List[Message]
624+
error_messages: typing.List[Message] = []
625625
if self.unique_items:
626626
seen_items = Uniqueness()
627627

typesystem/formats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
class BaseFormat:
23-
errors = {} # type: typing.Dict[str, str]
23+
errors: typing.Dict[str, str] = {}
2424

2525
def validation_error(self, code: str) -> ValidationError:
2626
text = self.errors[code].format(**self.__dict__)
@@ -80,7 +80,7 @@ def validate(self, value: typing.Any) -> datetime.time:
8080

8181
kwargs = {k: int(v) for k, v in groups.items() if v is not None}
8282
try:
83-
return datetime.time(**kwargs, tzinfo=None) # type: ignore
83+
return datetime.time(tzinfo=None, **kwargs)
8484
except ValueError:
8585
raise self.validation_error("invalid")
8686

typesystem/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def load_template_env(
121121
self, *, directory: str = None, package: str = None
122122
) -> "jinja2.Environment":
123123
if directory is not None and package is None:
124-
loader = jinja2.FileSystemLoader(directory) # type: jinja2.BaseLoader
124+
loader: jinja2.BaseLoader = jinja2.FileSystemLoader(directory)
125125
elif directory is None and package is not None:
126126
loader = jinja2.PackageLoader(package, "templates")
127127
else:

typesystem/json_schema.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def from_json_schema_type(
247247
elif type_string == "array":
248248
items = data.get("items", None)
249249
if items is None:
250-
items_argument = None # type: typing.Union[None, Field, typing.List[Field]]
250+
items_argument: typing.Union[None, Field, typing.List[Field]] = None
251251
elif isinstance(items, list):
252252
items_argument = [
253253
from_json_schema(item, definitions=definitions) for item in items
@@ -257,7 +257,7 @@ def from_json_schema_type(
257257

258258
additional_items = data.get("additionalItems", None)
259259
if additional_items is None:
260-
additional_items_argument = True # type: typing.Union[bool, Field]
260+
additional_items_argument: typing.Union[bool, Field] = True
261261
elif isinstance(additional_items, bool):
262262
additional_items_argument = additional_items
263263
else:
@@ -279,7 +279,7 @@ def from_json_schema_type(
279279
elif type_string == "object":
280280
properties = data.get("properties", None)
281281
if properties is None:
282-
properties_argument = None # type: typing.Optional[typing.Dict[str, Field]]
282+
properties_argument: typing.Optional[typing.Dict[str, Field]] = None
283283
else:
284284
properties_argument = {
285285
key: from_json_schema(value, definitions=definitions)
@@ -288,9 +288,9 @@ def from_json_schema_type(
288288

289289
pattern_properties = data.get("patternProperties", None)
290290
if pattern_properties is None:
291-
pattern_properties_argument = (
291+
pattern_properties_argument: typing.Optional[typing.Dict[str, Field]] = (
292292
None
293-
) # type: typing.Optional[typing.Dict[str, Field]]
293+
)
294294
else:
295295
pattern_properties_argument = {
296296
key: from_json_schema(value, definitions=definitions)
@@ -299,9 +299,7 @@ def from_json_schema_type(
299299

300300
additional_properties = data.get("additionalProperties", None)
301301
if additional_properties is None:
302-
additional_properties_argument = (
303-
None
304-
) # type: typing.Union[None, bool, Field]
302+
additional_properties_argument: typing.Union[None, bool, Field] = (None)
305303
elif isinstance(additional_properties, bool):
306304
additional_properties_argument = additional_properties
307305
else:
@@ -311,7 +309,7 @@ def from_json_schema_type(
311309

312310
property_names = data.get("propertyNames", None)
313311
if property_names is None:
314-
property_names_argument = None # type: typing.Optional[Field]
312+
property_names_argument: typing.Optional[Field] = None
315313
else:
316314
property_names_argument = from_json_schema(
317315
property_names, definitions=definitions
@@ -405,7 +403,7 @@ def to_json_schema(
405403
elif isinstance(arg, NeverMatch):
406404
return False
407405

408-
data = {} # type: dict
406+
data: dict = {}
409407
is_root = _definitions is None
410408
definitions = {} if _definitions is None else _definitions
411409

typesystem/schemas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def __new__(
5656
attrs: dict,
5757
definitions: SchemaDefinitions = None,
5858
) -> type:
59-
fields = {} # type: typing.Dict[str, Field]
59+
fields: typing.Dict[str, Field] = {}
6060

6161
for key, value in list(attrs.items()):
6262
if isinstance(value, Field):
@@ -90,7 +90,7 @@ def __new__(
9090

9191

9292
class Schema(Mapping, metaclass=SchemaMetaclass):
93-
fields = {} # type: typing.Dict[str, Field]
93+
fields: typing.Dict[str, Field] = {}
9494

9595
def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None:
9696
if args:

typesystem/tokenize/tokenize_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _TokenizingJSONObject(
2727
_ws: str = WHITESPACE_STR,
2828
) -> typing.Tuple[dict, int]:
2929
s, end = s_and_end
30-
pairs = [] # type: typing.List[typing.Tuple[Token, Token]]
30+
pairs: typing.List[typing.Tuple[Token, Token]] = []
3131
pairs_append = pairs.append
3232
memo_get = memo.setdefault
3333
# Use a slice to prevent IndexError from being raised, the following

typesystem/unique.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Uniqueness:
1313
FALSE = object()
1414

1515
def __init__(self, items: list = None) -> None:
16-
self._set = set() # type: set
16+
self._set: set = set()
1717
for item in items or []:
1818
self.add(item)
1919

0 commit comments

Comments
 (0)