Dealing with conflicting names
Suppose you have a class with a method whose name is the same as animported (or built-in) type, and you want to use the type in anothermethod signature. E.g.:
- 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 typebytes
as a reference to the method by that name. Other thanrenaming the method, a work-around is to use an alias:
- bytes_ = bytes
- class Message:
- def bytes(self):
- ...
- def register(self, path: bytes_):
- ...