napoleon: Don't use attribute type annotation for constructor parameters #11143
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix a bug where if a class has an attribute and a constructor parameter with the same name, napoleon will use the type annotation from the attribute for the constructor argument.
Feature or Bugfix
Detail
Suppose we have a class that looks like
The processed docstring looks like:
Note that the parameter has type
int
but Napoleon took the type from the attribute which has typebool
.A more practical reason this would happen is that the parameter has type
Optional[dict]
and this is stored into an attribute of typedict
where ifNone
is passed a default dictionary is created.I added a new parameter
is_attribute
to_lookup_annotation
and_consume_field
(s) which defaults toFalse
. Ifis_attribute
isFalse
andwhat
is"class"
then we look up the type from the constructor arguments instead. With these changes it produces the correct output: