Skip to content

Commit

Permalink
Explain how to work around a certain name conflict (#5099)
Browse files Browse the repository at this point in the history
Fixes #5097
  • Loading branch information
gvanrossum authored and ilevkivskyi committed May 24, 2018
1 parent 464b553 commit 7411cbe
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/source/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,32 @@ put the linter comment *after* the type comment:
.. code-block:: python
a = some_complex_thing() # type: ignore # noqa
Dealing with conflicting names
------------------------------

Suppose you have a class with a method whose name is the same as an
imported (or built-in) type, and you want to use the type in another
method signature. E.g.:

.. code-block:: python
class Message:
def bytes(self):
...
def register(self, path: bytes): # error: Invalid type "mod.Message.bytes"
...
The third line elicits an error because mypy sees the argument type
``bytes`` as a reference to the method by that name. Other than
renaming the method, a work-around is to use an alias:

.. code-block:: python
bytes_ = bytes
class Message:
def bytes(self):
...
def register(self, path: bytes_):
...

0 comments on commit 7411cbe

Please sign in to comment.