Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

napoleon: Don't use attribute type annotation for constructor parameters #11143

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hoodmane
Copy link

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

  • Bugfix

Detail

Suppose we have a class that looks like

class Example:
    """
    A Class

    Parameters
    ----------
    blah:
        Description of parameter blah
    """
    def __init__(self, blah: int):
        pass

    blah: bool

The processed docstring looks like:

A Class
       
:param blah: Description of parameter blah
:type blah: bool

Note that the parameter has type int but Napoleon took the type from the attribute which has type bool.
A more practical reason this would happen is that the parameter has type Optional[dict] and this is stored into an attribute of type dict where if None is passed a default dictionary is created.

I added a new parameter is_attribute to _lookup_annotation and _consume_field(s) which defaults to False. If is_attribute is False and what is "class" then we look up the type from the constructor arguments instead. With these changes it produces the correct output:

:param blah: Description of parameter blah
:type blah: int

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.

This changes it to instead look at the constructor type annotations
to find annotations for the constructor params.
@hoodmane hoodmane changed the title Don't use attribute type annotation for constructor parameters napoleon: Don't use attribute type annotation for constructor parameters Jan 20, 2023
@jfbu
Copy link
Contributor

jfbu commented Jan 22, 2023

merging into this the latest master update for CI checks

@AA-Turner AA-Turner added this to the some future version milestone Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants