| Conditions | 28 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 812 |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 LocalizedFileFieldForm.clean() 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 | 1 | from typing import List |
|
| 89 | def clean(self, value, initial=None): |
||
| 90 | """ |
||
| 91 | Most part of this method is a copy of |
||
| 92 | django.forms.MultiValueField.clean, with the exception of initial |
||
| 93 | value handling (this need for correct processing FileField's). |
||
| 94 | All original comments saved. |
||
| 95 | """ |
||
| 96 | if initial is None: |
||
| 97 | initial = [None for x in range(0, len(value))] |
||
| 98 | else: |
||
| 99 | if not isinstance(initial, list): |
||
| 100 | initial = self.widget.decompress(initial) |
||
| 101 | |||
| 102 | clean_data = [] |
||
| 103 | errors = [] |
||
| 104 | if not value or isinstance(value, (list, tuple)): |
||
| 105 | if (not value or not [v for v in value if |
||
| 106 | v not in self.empty_values]) \ |
||
| 107 | and (not initial or not [v for v in initial if |
||
| 108 | v not in self.empty_values]): |
||
| 109 | if self.required: |
||
| 110 | raise ValidationError(self.error_messages['required'], |
||
| 111 | code='required') |
||
| 112 | else: |
||
| 113 | raise ValidationError(self.error_messages['invalid'], |
||
| 114 | code='invalid') |
||
| 115 | for i, field in enumerate(self.fields): |
||
| 116 | try: |
||
| 117 | field_value = value[i] |
||
| 118 | except IndexError: |
||
| 119 | field_value = None |
||
| 120 | try: |
||
| 121 | field_initial = initial[i] |
||
| 122 | except IndexError: |
||
| 123 | field_initial = None |
||
| 124 | if field_value in self.empty_values and \ |
||
| 125 | field_initial in self.empty_values: |
||
| 126 | if self.require_all_fields: |
||
| 127 | # Raise a 'required' error if the MultiValueField is |
||
| 128 | # required and any field is empty. |
||
| 129 | if self.required: |
||
| 130 | raise ValidationError(self.error_messages['required'], |
||
| 131 | code='required') |
||
| 132 | elif field.required: |
||
| 133 | # Otherwise, add an 'incomplete' error to the list of |
||
| 134 | # collected errors and skip field cleaning, if a required |
||
| 135 | # field is empty. |
||
| 136 | if field.error_messages['incomplete'] not in errors: |
||
| 137 | errors.append(field.error_messages['incomplete']) |
||
| 138 | continue |
||
| 139 | try: |
||
| 140 | clean_data.append(field.clean(field_value, field_initial)) |
||
| 141 | except ValidationError as e: |
||
| 142 | # Collect all validation errors in a single list, which we'll |
||
| 143 | # raise at the end of clean(), rather than raising a single |
||
| 144 | # exception for the first error we encounter. Skip duplicates. |
||
| 145 | errors.extend(m for m in e.error_list if m not in errors) |
||
| 146 | if errors: |
||
| 147 | raise ValidationError(errors) |
||
| 148 | |||
| 149 | out = self.compress(clean_data) |
||
| 150 | self.validate(out) |
||
| 151 | self.run_validators(out) |
||
| 152 | return out |
||
| 153 | |||
| 167 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.