| Conditions | 11 |
| Total Lines | 51 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like structured_data.enum._process_class() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """Class decorator for defining abstract data types.""" |
||
| 151 | def _process_class(_cls, _repr, eq, order): |
||
| 152 | if order and not eq: |
||
| 153 | raise ValueError('eq must be true if order is true') |
||
| 154 | |||
| 155 | args = {} |
||
| 156 | subclasses = set() |
||
| 157 | subclass_order = [] |
||
| 158 | for cls in reversed(_cls.__mro__): |
||
| 159 | for key, value in getattr(cls, '__annotations__', {}).items(): |
||
| 160 | _nillable_write( |
||
| 161 | args, key, _args(value, vars(sys.modules[cls.__module__]))) |
||
| 162 | |||
| 163 | for name, args_ in args.items(): |
||
| 164 | make_constructor(_cls, name, args_, subclasses, subclass_order) |
||
| 165 | |||
| 166 | SUBCLASS_ORDER[_cls] = tuple(subclass_order) |
||
| 167 | |||
| 168 | _cls.__init_subclass__ = PrewrittenMethods.__init_subclass__ |
||
| 169 | |||
| 170 | if _set_new_functions(_cls, _make_nested_new(_cls, subclasses, _enum_super(_cls))): |
||
| 171 | _cls.__new__ = _make_nested_new(_cls, subclasses, _cls.__new__) |
||
| 172 | |||
| 173 | _set_new_functions( |
||
| 174 | _cls, PrewrittenMethods.__setattr__, PrewrittenMethods.__delattr__) |
||
| 175 | _set_new_functions(_cls, PrewrittenMethods.__bool__) |
||
| 176 | |||
| 177 | _add_repr(_cls, _repr) |
||
| 178 | |||
| 179 | equality_methods_were_set = _add_eq(_cls, eq) |
||
| 180 | |||
| 181 | if equality_methods_were_set: |
||
| 182 | _cls.__hash__ = PrewrittenMethods.__hash__ |
||
| 183 | |||
| 184 | if order: |
||
| 185 | if not equality_methods_were_set: |
||
| 186 | raise ValueError( |
||
| 187 | "Can't add ordering methods if equality methods are provided.") |
||
| 188 | collision = _set_new_functions( |
||
| 189 | _cls, |
||
| 190 | PrewrittenMethods.__lt__, |
||
| 191 | PrewrittenMethods.__le__, |
||
| 192 | PrewrittenMethods.__gt__, |
||
| 193 | PrewrittenMethods.__ge__ |
||
| 194 | ) |
||
| 195 | if collision: |
||
| 196 | raise TypeError( |
||
| 197 | 'Cannot overwrite attribute {collision} in class ' |
||
| 198 | '{name}. Consider using functools.total_ordering'.format( |
||
| 199 | collision=collision, name=_cls.__name__)) |
||
| 200 | |||
| 201 | return _cls |
||
| 202 | |||
| 218 |