| Conditions | 14 |
| Total Lines | 60 |
| Code Lines | 45 |
| 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.""" |
||
| 128 | def _process_class(_cls, _repr, eq, order): |
||
| 129 | if order and not eq: |
||
| 130 | raise ValueError('eq must be true if order is true') |
||
| 131 | |||
| 132 | argses = {} |
||
| 133 | subclasses = set() |
||
| 134 | subclass_order = [] |
||
| 135 | for cls in reversed(_cls.__mro__): |
||
| 136 | for key, value in getattr(cls, '__annotations__', {}).items(): |
||
| 137 | args = _args(value, vars(sys.modules[cls.__module__])) |
||
| 138 | # Shadow redone annotations. |
||
| 139 | if args is None: |
||
| 140 | argses.pop(key, None) |
||
| 141 | else: |
||
| 142 | argses[key] = args |
||
| 143 | |||
| 144 | for name, args in argses.items(): |
||
| 145 | make_constructor(_cls, name, args, subclasses, subclass_order) |
||
| 146 | |||
| 147 | _cls.__init_subclass__ = PrewrittenMethods.__init_subclass__ |
||
| 148 | |||
| 149 | if _set_new_functions(_cls, _make_nested_new(_cls, subclasses, _enum_super(_cls))): |
||
| 150 | _cls.__new__ = _make_nested_new(_cls, subclasses, _cls.__new__) |
||
| 151 | |||
| 152 | _set_new_functions( |
||
| 153 | _cls, PrewrittenMethods.__setattr__, PrewrittenMethods.__delattr__) |
||
| 154 | _set_new_functions(_cls, PrewrittenMethods.__bool__) |
||
| 155 | |||
| 156 | if _repr: |
||
| 157 | _set_new_functions(_cls, PrewrittenMethods.__repr__) |
||
| 158 | |||
| 159 | equality_methods_were_set = False |
||
| 160 | |||
| 161 | if eq: |
||
| 162 | equality_methods_were_set = not _set_new_functions( |
||
| 163 | _cls, PrewrittenMethods.__eq__, PrewrittenMethods.__ne__) |
||
| 164 | |||
| 165 | if equality_methods_were_set: |
||
| 166 | _cls.__hash__ = PrewrittenMethods.__hash__ |
||
| 167 | |||
| 168 | if order: |
||
| 169 | if not equality_methods_were_set: |
||
| 170 | raise ValueError( |
||
| 171 | "Can't add ordering methods if equality methods are provided.") |
||
| 172 | collision = _set_new_functions( |
||
| 173 | _cls, |
||
| 174 | PrewrittenMethods.__lt__, |
||
| 175 | PrewrittenMethods.__le__, |
||
| 176 | PrewrittenMethods.__gt__, |
||
| 177 | PrewrittenMethods.__ge__ |
||
| 178 | ) |
||
| 179 | if collision: |
||
| 180 | raise TypeError( |
||
| 181 | 'Cannot overwrite attribute {collision} in class ' |
||
| 182 | '{name}. Consider using functools.total_ordering'.format( |
||
| 183 | collision=collision, name=_cls.__name__)) |
||
| 184 | |||
| 185 | SUBCLASS_ORDER[_cls] = tuple(subclass_order) |
||
| 186 | |||
| 187 | return _cls |
||
| 188 | |||
| 204 |