question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated constructors incorrectly accept positional arguments

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nipunn1313commented, May 26, 2019

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.

0reactions
erictrautcommented, May 24, 2019

Thanks! Much appreciated.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found