Skip to content

Commit 401eeff

Browse files
authored
Merge pull request #315 from VYaswanthKumar/VYaswanthKumar-patch-1
Fix #242: Add type parameters to PRecord for pylance/pyright compatib…
2 parents 4c95b35 + 88e2884 commit 401eeff

2 files changed

Lines changed: 6 additions & 12 deletions

File tree

pyrsistent/_checked_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CheckedType(object):
2121

2222
@classmethod
2323
@abstractmethod
24-
def create(cls, source_data, _factory_fields=None):
24+
def create(cls, source_data, _factory_fields=None, ignore_extra=False):
2525
raise NotImplementedError()
2626

2727
@abstractmethod
@@ -498,7 +498,7 @@ def serialize(self, format=None):
498498
return dict(serializer(format, k, v) for k, v in self.items())
499499

500500
@classmethod
501-
def create(cls, source_data, _factory_fields=None):
501+
def create(cls, source_data, _factory_fields=None, ignore_extra=False):
502502
if isinstance(source_data, cls):
503503
return source_data
504504

pyrsistent/_precord.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Any
12
from pyrsistent._checked_types import CheckedType, _restore_pickle, InvariantException, store_invariants
23
from pyrsistent._field_common import (
34
set_fields, check_type, is_field_ignore_extra_complaint, PFIELD_NO_INITIAL, serialize, check_global_invariants
@@ -9,20 +10,15 @@ class _PRecordMeta(type):
910
def __new__(mcs, name, bases, dct):
1011
set_fields(dct, bases, name='_precord_fields')
1112
store_invariants(dct, bases, '_precord_invariants', '__invariant__')
12-
1313
dct['_precord_mandatory_fields'] = \
1414
set(name for name, field in dct['_precord_fields'].items() if field.mandatory)
15-
1615
dct['_precord_initial_values'] = \
1716
dict((k, field.initial) for k, field in dct['_precord_fields'].items() if field.initial is not PFIELD_NO_INITIAL)
18-
19-
2017
dct['__slots__'] = ()
21-
2218
return super(_PRecordMeta, mcs).__new__(mcs, name, bases, dct)
2319

2420

25-
class PRecord(PMap, CheckedType, metaclass=_PRecordMeta):
21+
class PRecord(PMap[str, Any], CheckedType, metaclass=_PRecordMeta):
2622
"""
2723
A PRecord is a PMap with a fixed set of specified fields. Records are declared as python classes inheriting
2824
from PRecord. Because it is a PMap it has full support for all Mapping methods such as iteration and element
@@ -39,7 +35,6 @@ def __new__(cls, **kwargs):
3935

4036
factory_fields = kwargs.pop('_factory_fields', None)
4137
ignore_extra = kwargs.pop('_ignore_extra', False)
42-
4338
initial_values = kwargs
4439
if cls._precord_initial_values:
4540
initial_values = dict((k, v() if callable(v) else v)
@@ -58,7 +53,6 @@ def set(self, *args, **kwargs):
5853
class. First of all it accepts key-value pairs. Second it accepts multiple key-value
5954
pairs to perform one, atomic, update of multiple fields.
6055
"""
61-
6256
# The PRecord set() can accept kwargs since all fields that have been declared are
6357
# valid python identifiers. Also allow multiple fields to be set in one operation.
6458
if args:
@@ -124,7 +118,7 @@ def set(self, key, original_value):
124118
if field:
125119
if self._factory_fields is None or field in self._factory_fields:
126120
try:
127-
if is_field_ignore_extra_complaint(PRecord, field, self._ignore_extra):
121+
if is_field_ignore_extra_complaint(CheckedType, field, self._ignore_extra):
128122
value = field.factory(original_value, ignore_extra=self._ignore_extra)
129123
else:
130124
value = field.factory(original_value)
@@ -136,7 +130,6 @@ def set(self, key, original_value):
136130
value = original_value
137131

138132
check_type(self._destination_cls, field, key, value)
139-
140133
is_ok, error_code = field.invariant(value)
141134
if not is_ok:
142135
self._invariant_error_codes.append(error_code)
@@ -149,6 +142,7 @@ def persistent(self):
149142
cls = self._destination_cls
150143
is_dirty = self.is_dirty()
151144
pm = super(_PRecordEvolver, self).persistent()
145+
152146
if is_dirty or not isinstance(pm, cls):
153147
result = cls(_precord_buckets=pm._buckets, _precord_size=pm._size)
154148
else:

0 commit comments

Comments
 (0)