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

Complaint about @staticmethod outside a class #1006

Open
gvanrossum opened this issue Nov 25, 2015 · 3 comments
Open

Complaint about @staticmethod outside a class #1006

gvanrossum opened this issue Nov 25, 2015 · 3 comments
Labels
false-positive mypy gave an error on correct code feature priority-2-low

Comments

@gvanrossum
Copy link
Member

I found code like this:

def foo():
    @staticmethod
    def bar():
        pass

This gets an error:

/Users/guido/mypy_tests/mypy_static.py: note: In function "foo":
/Users/guido/mypy_tests/mypy_static.py:2: error: 'staticmethod' used with a non-method

It can be silenced by putting # type: ignore on the @staticmethod line.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 26, 2015

What's the purpose of that code? Does it return the function?

@refi64
Copy link
Contributor

refi64 commented Nov 26, 2015

I think this is probably to add static methods to a class dynamically? Like:

MyClass.bar = bar

@gvanrossum
Copy link
Member Author

The pattern is like this:

    def _create_accessor(col):
        @staticmethod  # type: ignore
        def fn(row):
            try:
                return row[col]
            except (KeyError, IndexError):
                return None
        return fn

    _get_updated_host_id = _create_accessor('updated_host_id')
    _get_local_host_id = _create_accessor('local_host_id')

(and lots more like those last two lines).

@gvanrossum gvanrossum added this to the Future milestone Apr 14, 2016
@gvanrossum gvanrossum removed this from the Future milestone Mar 29, 2017
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive mypy gave an error on correct code feature priority-2-low
Projects
None yet
Development

No branches or pull requests

3 participants