Generated constructors incorrectly accept positional arguments
See original GitHub issueThe annotated __init__
method generated for a protobuf message accepts positional arguments, but the underlying implementation accepts only named arguments. The first parameter after self
should be a nameless *
parameter. This will tell mypy and other type checkers that positional arguments are not allowed when calling the constructor.
For example, a protobuf message with a string: name
field will generate the following annotated stub:
class MyMessage(google___protobuf___message___Message):
name = ... # type: typing___Text
def __init__(self,
name : typing___Optional[typing___Text] = None) -> None: ...
If callers attempt to construct this message by calling MyMessage('bob')
, a runtime error occurs indicating that positional arguments are not allowed for this call.
To match the underlying implementation, it should be:
def __init__(self, *,
name : typing___Optional[typing___Text] = None) -> None: ...
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
False positive on arguments of constructor created by "attrs"
No error should be printed, as the generated constructor takes two arguments ( self and value ). pylint --version output. pylint 2.4.4 astroid ......
Read more >Subclass constructor throws TypeError: __init__() takes 2 ...
By using *args, **kwargs , the child class will pass all uncaptured positional and named arguments to the superclass init method.
Read more >Positional and Keyword Arguments
Python functions can contain two types of arguments: positional arguments and keyword arguments. Positional arguments must be included in the correct order.
Read more >Default function arguments are the devil – Arthur O'Dwyer
But in C++11 and later, we have delegating constructors. We can write this code straightforwardly, turning init back into a proper constructor.
Read more >optparse — Parser for command line options — Python 3.11.1 ...
The OptionParser constructor has no required arguments, but a number of optional keyword arguments. You should always pass them as keyword arguments, i.e....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This generated constructors issue should be fixed with #74.
The virtualenvironment issue was quite interesting and should be fixed with #76. Didn’t actually have to do with py2/py3, but rather with google’s new release of the python library python-protobuf (this project didn’t pin the version!). My setup continued to work because I still had the old version installed.
Thanks! Much appreciated.