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

Overloaded functions and methods with type annotations trigger F811 #230

Closed
asottile opened this issue Apr 3, 2021 · 16 comments
Closed

Comments

@asottile
Copy link
Member

asottile commented Apr 3, 2021

In GitLab by @wjwwood on May 23, 2018, 23:20

I installed flake8 with:

% sudo -H python3 -m pip install -U flake8

I ran my examples on macOS 10.13.3 using Python 3.6.5 from Homebrew.

Please provide the exact, unmodified output of flake8 --bug-report

% flake8 --bug-report
{
  "dependencies": [
    {
      "dependency": "setuptools",
      "version": "39.0.1"
    }
  ],
  "platform": {
    "python_implementation": "CPython",
    "python_version": "3.6.5",
    "system": "Darwin"
  },
  "plugins": [
    {
      "is_local": false,
      "plugin": "flake8-blind-except",
      "version": "0.1.1"
    },
    {
      "is_local": false,
      "plugin": "flake8-comprehensions",
      "version": "1.4.1"
    },
    {
      "is_local": false,
      "plugin": "flake8-docstrings",
      "version": "1.3.0, pydocstyle: 2.1.1"
    },
    {
      "is_local": false,
      "plugin": "flake8_builtins",
      "version": "0.3"
    },
    {
      "is_local": false,
      "plugin": "flake8_deprecated",
      "version": "1.2"
    },
    {
      "is_local": false,
      "plugin": "flake8_quotes",
      "version": "0.13.0"
    },
    {
      "is_local": false,
      "plugin": "import-order",
      "version": "0.17"
    },
    {
      "is_local": false,
      "plugin": "mccabe",
      "version": "0.6.1"
    },
    {
      "is_local": false,
      "plugin": "new_line_checker",
      "version": "1.6.0"
    },
    {
      "is_local": false,
      "plugin": "pycodestyle",
      "version": "2.3.1"
    },
    {
      "is_local": false,
      "plugin": "pyflakes",
      "version": "1.6.0"
    }
  ],
  "version": "3.5.0"
}

Feature request or bug report:

When using type annotations (Python 3.5+), you can use the typing.overload decorator to redefine functions or methods in order to express how different overloads can map to different return types. Here's a working example based on the example from the Python documentation (https://docs.python.org/3/library/typing.html#typing.overload):

"""Example of type annotations with overload triggering redefinition."""

from typing import Tuple
from typing import overload


@overload
def process(response: None) -> None:
    """Specialize for None."""
    ...


@overload
def process(response: int) -> Tuple[int, str]:
    """Specialize for int."""
    ...


@overload
def process(response: bytes) -> str:
    """Specialize for bytes."""
    ...


def process(response):
    """Implement."""
    print(response)


if __name__ == '__main__':
    process('test')

If you run this (python3 ./example.py) it works:

% python3 ./example.py
test

But if you run flake8 on it you get F811 triggered:

% flake8 example.py
example.py:13:1: F811 redefinition of unused 'process' from line 7
example.py:19:1: F811 redefinition of unused 'process' from line 13
example.py:25:1: F811 redefinition of unused 'process' from line 19

Which I suppose makes sense, and I could disable this check explicitly, but I also think it's a valid use case and pattern so it would be nice if flake8 could use the @overload decorator as a hint to suppress F811 automatically.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @wjwwood on May 23, 2018, 23:20

changed the description

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Oct 13, 2018, 18:09

This is an issue with upstream pyflakes -- I'd suggest following this issue

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Oct 23, 2018, 23:51

(closing given the pyflakes issue)

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Oct 23, 2018, 23:51

closed

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @erichiller on Jan 27, 2019, 10:53

It appears the issue has been resolved, and new PyFlakes 2.1.0 released, but I am still having the same issue with Overloads getting F811

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Jan 27, 2019, 11:00

We have not made a release enabling the new pyflakes yet. It's in progress though, I'm currently waiting on pycodestyle to make a release as well so we can batch up the bumps in the same release

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @Melevir on May 13, 2019, 22:35

We have not made a release enabling the new pyflakes yet. It's in progress though, I'm currently waiting on pycodestyle to make a release as well so we can batch up the bumps in the same release

Any progress here? Maybe I can help somehow?

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on May 14, 2019, 24:31

Yes it's fixed, please upgrade to latest

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @douardda1 on Apr 2, 2020, 08:17

Looks to me it's not fixed for methods. Am I missing something?

Given a python3.7 (3.7.3, debian buster) venv in which I have:

flake8                      3.7.9          
pyflakes                    2.1.1          

and this 'test.py' file:

from typing import overload


@overload
def test1(arg: str) -> None:
    ...


@overload
def test1(arg: int) -> None:
    ...


def test1(arg) -> None:
    print(arg)

class A:
    @overload
    def test1(self, arg: str) -> None:
        ...

    @overload
    def test1(self, arg: int) -> None:
        ...

    def test1(self, arg) -> None:
        print(arg)

then:

flake8 test.py 
test.py:23:5: F811 redefinition of unused 'test1' from line 19
test.py:27:5: F811 redefinition of unused 'test1' from line 23

Thanks,
David

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Apr 2, 2020, 08:19

it's fixed on master in pyflakes

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @douardda1 on Apr 2, 2020, 08:45

I guess this might answer my next question :-)

Thanks

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @adithyabsk on Apr 11, 2020, 19:41

@asottile Can we leave this issue open until the version of pyflakes is updated? It'll help with people tracking this issue.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Apr 11, 2020, 19:49

  • pyflakes 2.1.x is already in released flake8 3.7.x (what the original issue is about)
  • I'm not going to reopen this (or open an issue for every downstream issue that gets fixed since they're not issues on this repository itself)
  • if you're interested in updates, I'd suggest following the releases of this repository -- that's far more likely to get activity than this issue anyway upon release

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @adithyabsk on Apr 11, 2020, 19:53

I guess that's fair enough.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Apr 11, 2020, 19:56

consider also watching !417 for pyflakes specifically, though I'm not sure that helps you all that much

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @adithyabsk on Apr 11, 2020, 19:58

@asottile Actually's that more helpful and what I was looking for. I can peg to master once that's merged in.

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

No branches or pull requests

1 participant