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

False positive W0231 when using typing.overload. #3020

Closed
mrshannon opened this issue Jul 22, 2019 · 6 comments · Fixed by #3055
Closed

False positive W0231 when using typing.overload. #3020

mrshannon opened this issue Jul 22, 2019 · 6 comments · Fixed by #3055
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors

Comments

@mrshannon
Copy link

Steps to reproduce

Minimum failing example.

from typing import Union, overload


class Parent:

    def __init__(self, num: int) -> None:
        self.number = num


class Child(Parent):

    @overload
    def __init__(self, num: int) -> None:
        ...

    @overload
    def __init__(self, num: float) -> None:
        ...

    def __init__(self, num: Union[int, float]):
        super().__init__(round(num))

Run pylint on the above example code.

Current behavior

pylint_init_bug.py:13:4: W0231: __init__ method from base class 'Parent' is not called (super-init-not-called)
pylint_init_bug.py:17:4: W0231: __init__ method from base class 'Parent' is not called (super-init-not-called)

Expected behavior

The methods decorated with typing.overload should not be required to call the base __init__ as they are "for the benefit of the type checker only" as indicated at https://docs.python.org/3/library/typing.html#typing.overload.

pylint --version output

pylint 2.3.1
astroid 2.2.5
Python 3.7.3 (default, Apr  3 2019, 19:16:38) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]

Same bug exists on the master branch which includes #2927.

pylint 2.4.0-dev0
astroid 2.3.0
Python 3.7.3 (default, Apr  3 2019, 19:16:38) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
@PCManticore
Copy link
Contributor

Thanks, this definitely makes sense.

@PCManticore PCManticore added Good first issue Friendly and approachable by new contributors Enhancement ✨ Improvement to a component labels Aug 9, 2019
@gyermolenko
Copy link
Contributor

I can try and take a look

@gyermolenko
Copy link
Contributor

gyermolenko commented Aug 10, 2019

@mrshannon

def __init__(self, num: Union[int, float]):
    super().__init__(round(num))

I think you don't need to put type annotations here, it was the sole purpose of overload methods to do that.
Also, for some time now It is not required to specify return type of __init__.

@mrshannon
Copy link
Author

@gyermolenko
You are correct about the return value of __init__. However, the Union is required. The CPython documentation for the typing module is incorrect in several areas (see python/mypy#5466).

However, none of these changes prevent the original warnings from pylint.

@gyermolenko
Copy link
Contributor

However, none of these changes prevent the original warnings from pylint

Well, to fix the issue I've created PR :)

@mrshannon
Copy link
Author

@gyermolenko
I think I see now. While the above is technically valid it's not required since __init__ always has the same return type. My mistake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants