pyderive3 0.0.1__tar.gz → 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyderive3
3
- Version: 0.0.1
3
+ Version: 0.0.2
4
4
  Summary: Python3 Custom Data Class Helpers
5
5
  Home-page: https://github.com/imgurbot12/pyderive
6
6
  Author: Andrew Scott
@@ -13,10 +13,16 @@ Requires-Python: >=3.8
13
13
  Description-Content-Type: text/markdown
14
14
 
15
15
  pyderive
16
- -------------
16
+ ---------
17
17
 
18
18
  Python3 Custom Dataclass Helpers
19
19
 
20
+ ### Installation
21
+
22
+ ```bash
23
+ pip install pyderive3
24
+ ```
25
+
20
26
  ### Features
21
27
 
22
28
  ##### Expanded Reimplementation of Dataclasses
@@ -1,8 +1,14 @@
1
1
  pyderive
2
- -------------
2
+ ---------
3
3
 
4
4
  Python3 Custom Dataclass Helpers
5
5
 
6
+ ### Installation
7
+
8
+ ```bash
9
+ pip install pyderive3
10
+ ```
11
+
6
12
  ### Features
7
13
 
8
14
  ##### Expanded Reimplementation of Dataclasses
@@ -62,11 +62,15 @@ class FieldDef(Protocol):
62
62
  kw_only: bool = False
63
63
  frozen: bool = False
64
64
  field_type: FieldType = FieldType.STANDARD
65
-
65
+
66
66
  @abstractmethod
67
67
  def __init__(self, name: str, anno: Type, default: Any = MISSING):
68
68
  raise NotImplementedError
69
69
 
70
+ def finalize(self):
71
+ """run finalize when field variables are finished compiling"""
72
+ pass
73
+
70
74
  class Field(FieldDef):
71
75
 
72
76
  def __init__(self,
@@ -191,17 +191,19 @@ def create_hash(fields: Fields) -> Callable:
191
191
  names_t = _tuple_str(names, 'self')
192
192
  return _create_fn('__hash__', ['self'], [f'return hash({names_t})'])
193
193
 
194
- def assign_func(cls: Type, func: Callable, name: Optional[str] = None) -> bool:
194
+ def assign_func(cls: Type, func: Callable,
195
+ name: Optional[str] = None, overwrite: bool = False) -> bool:
195
196
  """
196
197
  assign function to the object and modify qualname
197
198
 
198
- :param cls: class object to assign to
199
- :param func: function to assign to value
200
- :param name: name of function to assign
201
- :return: true if object already exists else false
199
+ :param cls: class object to assign to
200
+ :param func: function to assign to value
201
+ :param name: name of function to assign
202
+ :param overwrite: allow for overwrite if enabled
203
+ :return: true if object already exists else false
202
204
  """
203
205
  name = name or func.__name__
204
- if name in cls.__dict__:
206
+ if not overwrite and name in cls.__dict__:
205
207
  return True
206
208
  if isinstance(func, FunctionType):
207
209
  func.__qualname__ = f'{cls.__qualname__}.{func.__name__}'
@@ -4,7 +4,7 @@ DataClass stdlib recreation using existing helpers
4
4
  import abc
5
5
  import copy
6
6
  from typing import *
7
- from typing_extensions import dataclass_transform
7
+ from typing_extensions import dataclass_transform, runtime_checkable
8
8
 
9
9
  from .abc import *
10
10
  from .parse import *
@@ -62,6 +62,7 @@ HASH_ACTIONS: Dict[Tuple[bool, bool, bool, bool], Any] = {
62
62
  (True, True, True, True ): _hash_err,
63
63
  }
64
64
 
65
+ @runtime_checkable
65
66
  class DataClassLike(Generic[F], Protocol):
66
67
  """Protocol for DataClass-Like Objects"""
67
68
  __datafields__: List[F]
@@ -143,8 +144,8 @@ def asdict(cls, *, recurse: int = 0, dict_factory: Type[dict] = dict) -> dict:
143
144
  @dataclass_transform(field_specifiers=(FieldDef, Field, field))
144
145
  def _process_class(
145
146
  cls: TypeT,
146
- init: bool = True,
147
- repr: bool = True,
147
+ init: Optional[bool] = None,
148
+ repr: Optional[bool] = None,
148
149
  eq: bool = True,
149
150
  order: bool = False,
150
151
  unsafe_hash: bool = False,
@@ -165,11 +166,14 @@ def _process_class(
165
166
  # assign fields to dataclass
166
167
  setattr(cls, FIELD_ATTR, fields)
167
168
  # build functions
168
- if init:
169
+ if init or init is None:
170
+ overwrite = init is True
169
171
  post_init = hasattr(cls, POST_INIT)
170
- assign_func(cls, create_init(fields, kw_only, post_init, frozen))
171
- if repr:
172
- assign_func(cls, create_repr(fields))
172
+ initfunc = create_init(fields, kw_only, post_init, frozen)
173
+ assign_func(cls, initfunc, overwrite=overwrite)
174
+ if repr or repr is None:
175
+ overwrite = repr is True
176
+ assign_func(cls, create_repr(fields), overwrite=overwrite)
173
177
  if eq:
174
178
  assign_func(cls, create_compare(fields, '__eq__', '=='))
175
179
  if order:
@@ -207,8 +211,8 @@ def _process_class(
207
211
  @overload
208
212
  @dataclass_transform(field_specifiers=(FieldDef, Field, field))
209
213
  def dataclass(cls: Type[T],
210
- init: bool = True,
211
- repr: bool = True,
214
+ init: Optional[bool] = None,
215
+ repr: Optional[bool] = None,
212
216
  eq: bool = True,
213
217
  order: bool = False,
214
218
  unsafe_hash: bool = False,
@@ -223,8 +227,8 @@ def dataclass(cls: Type[T],
223
227
 
224
228
  @overload
225
229
  def dataclass(*,
226
- init: bool = True,
227
- repr: bool = True,
230
+ init: Optional[bool] = None,
231
+ repr: Optional[bool] = None,
228
232
  eq: bool = True,
229
233
  order: bool = False,
230
234
  unsafe_hash: bool = False,
@@ -95,6 +95,8 @@ def parse_fields(
95
95
  if not isinstance(anno, type) and isinstance(anno, InitVar):
96
96
  field.anno = anno.type
97
97
  field.field_type = FieldType.INIT_VAR
98
+ # finalize field build and assign to struct
99
+ field.finalize()
98
100
  fields.fields[name] = field
99
101
  # apply fields to baseclass to allow for inheritance
100
102
  if fields.fields:
@@ -132,7 +134,7 @@ def flatten_fields(
132
134
  for name in fields.order:
133
135
  field = fields.fields[name]
134
136
  default = has_default(field)
135
- kwargs = kwargs or default
137
+ kwargs = kwargs or (default and field.init and not field.kw_only)
136
138
  missing = name not in struct.fields
137
139
  # raise error if non-kwarg found after kwargs start
138
140
  if order_kw and kwargs and missing and not default:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyderive3
3
- Version: 0.0.1
3
+ Version: 0.0.2
4
4
  Summary: Python3 Custom Data Class Helpers
5
5
  Home-page: https://github.com/imgurbot12/pyderive
6
6
  Author: Andrew Scott
@@ -13,10 +13,16 @@ Requires-Python: >=3.8
13
13
  Description-Content-Type: text/markdown
14
14
 
15
15
  pyderive
16
- -------------
16
+ ---------
17
17
 
18
18
  Python3 Custom Dataclass Helpers
19
19
 
20
+ ### Installation
21
+
22
+ ```bash
23
+ pip install pyderive3
24
+ ```
25
+
20
26
  ### Features
21
27
 
22
28
  ##### Expanded Reimplementation of Dataclasses
@@ -5,7 +5,7 @@ with open("README.md", "r") as f:
5
5
 
6
6
  setup(
7
7
  name='pyderive3',
8
- version='0.0.1',
8
+ version='0.0.2',
9
9
  license='MIT',
10
10
  author='Andrew Scott',
11
11
  author_email='imgurbot12@gmail.com',
File without changes