Skip to content

Commit b8389d0

Browse files
type-annotate Record
1 parent eee40e0 commit b8389d0

3 files changed

Lines changed: 18 additions & 21 deletions

File tree

pytools/__init__.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import sys
3636
from functools import reduce, wraps
3737
from sys import intern
38-
from typing import (
38+
from typing import (cast,
3939
Any, Callable, ClassVar, Dict, Generic, Hashable, Iterable, Iterator, List,
4040
Mapping, Optional, Sequence, Set, Tuple, Type, TypeVar, Union)
4141

@@ -413,7 +413,8 @@ class RecordWithoutPickling:
413413
__slots__: ClassVar[List[str]] = []
414414
fields: ClassVar[Set[str]]
415415

416-
def __init__(self, valuedict=None, exclude=None, **kwargs):
416+
def __init__(self, valuedict: Optional[Dict[str, Any]] = None,
417+
exclude: Optional[List[str]] = None, **kwargs: Any) -> None:
417418
assert self.__class__ is not Record
418419

419420
if exclude is None:
@@ -432,7 +433,7 @@ def __init__(self, valuedict=None, exclude=None, **kwargs):
432433
fields.add(key)
433434
setattr(self, key, value)
434435

435-
def get_copy_kwargs(self, **kwargs):
436+
def get_copy_kwargs(self, **kwargs: Any) -> Dict[str, Any]:
436437
for f in self.__class__.fields:
437438
if f not in kwargs:
438439
try:
@@ -441,25 +442,25 @@ def get_copy_kwargs(self, **kwargs):
441442
pass
442443
return kwargs
443444

444-
def copy(self, **kwargs):
445+
def copy(self, **kwargs: Any) -> "RecordWithoutPickling":
445446
return self.__class__(**self.get_copy_kwargs(**kwargs))
446447

447-
def __repr__(self):
448+
def __repr__(self) -> str:
448449
return "{}({})".format(
449450
self.__class__.__name__,
450451
", ".join(f"{fld}={getattr(self, fld)!r}"
451452
for fld in self.__class__.fields
452453
if hasattr(self, fld)))
453454

454-
def register_fields(self, new_fields):
455+
def register_fields(self, new_fields: Set[str]) -> None:
455456
try:
456457
fields = self.__class__.fields
457458
except AttributeError:
458459
self.__class__.fields = fields = set()
459460

460461
fields.update(new_fields)
461462

462-
def __getattr__(self, name):
463+
def __getattr__(self, name: str) -> Any:
463464
# This method is implemented to avoid pylint 'no-member' errors for
464465
# attribute access.
465466
raise AttributeError(
@@ -470,13 +471,13 @@ def __getattr__(self, name):
470471
class Record(RecordWithoutPickling):
471472
__slots__: ClassVar[List[str]] = []
472473

473-
def __getstate__(self):
474+
def __getstate__(self) -> Dict[str, Any]:
474475
return {
475476
key: getattr(self, key)
476477
for key in self.__class__.fields
477478
if hasattr(self, key)}
478479

479-
def __setstate__(self, valuedict):
480+
def __setstate__(self, valuedict: Dict[str, Any]) -> None:
480481
try:
481482
fields = self.__class__.fields
482483
except AttributeError:
@@ -486,30 +487,27 @@ def __setstate__(self, valuedict):
486487
fields.add(key)
487488
setattr(self, key, value)
488489

489-
def __eq__(self, other):
490+
def __eq__(self, other: object) -> bool:
490491
if self is other:
491492
return True
492493
return (self.__class__ == other.__class__
493494
and self.__getstate__() == other.__getstate__())
494495

495-
def __ne__(self, other):
496-
return not self.__eq__(other)
497-
498496

499497
class ImmutableRecordWithoutPickling(RecordWithoutPickling):
500498
"""Hashable record. Does not explicitly enforce immutability."""
501-
def __init__(self, *args, **kwargs):
499+
def __init__(self, *args: Any, **kwargs: Any) -> None:
502500
RecordWithoutPickling.__init__(self, *args, **kwargs)
503-
self._cached_hash = None
501+
self._cached_hash: Optional[int] = None
504502

505-
def __hash__(self):
503+
def __hash__(self) -> int:
506504
# This attribute may vanish during pickling.
507505
if getattr(self, "_cached_hash", None) is None:
508506
self._cached_hash = hash(
509507
(type(self),) + tuple(getattr(self, field)
510508
for field in self.__class__.fields))
511509

512-
return self._cached_hash
510+
return cast(int, self._cached_hash)
513511

514512

515513
class ImmutableRecord(ImmutableRecordWithoutPickling, Record):

pytools/datatable.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"""
1212

1313

14-
# type-ignore-reason: Record is untyped
15-
class Row(Record): # type: ignore[misc]
14+
class Row(Record):
1615
pass
1716

1817

run-mypy.sh

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

33
set -ex
44

5-
mypy --show-error-codes pytools
5+
mypy pytools
66

7-
mypy --strict --follow-imports=skip pytools/datatable.py
7+
mypy --strict pytools/datatable.py

0 commit comments

Comments
 (0)