Skip to content

Incorrect errors logged by the positional_decorator #825

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

Closed
hiranya911 opened this issue Feb 18, 2020 · 1 comment
Closed

Incorrect errors logged by the positional_decorator #825

hiranya911 opened this issue Feb 18, 2020 · 1 comment
Assignees
Labels
type: question Request for information or clarification. Not an issue.

Comments

@hiranya911
Copy link

Calling http.BatchHttpRequest() with only positional arguments raises/logs the following error:

req = BatchHttpRequest(callback, my_batch_uri)

TypeError: __init__() takes at most 1 positional argument (3 given)

This makes no sense since I'm only passing 2 arguments to the constructor.

Moreover, the decorator on the constructor indicates positional(1), which I assume means should allow at most one positional argument. But the following doesn't work:

req = BatchHttpRequest(callback, batch_uri=my_batch_uri)

TypeError: __init__() takes at most 1 positional argument (2 given)
@busunkim96
Copy link
Contributor

It looks like that decorator was written to force arguments to be passed by name. The number '1' in positional(1) means 'allow 1 positional argument'. In this case the one allowed positional argument is self. Everything after it must be passed as a keyword.

def positional(max_positional_args):
"""A decorator to declare that only the first N arguments my be positional.
This decorator makes it easy to support Python 3 style keyword-only
parameters. For example, in Python 3 it is possible to write::
def fn(pos1, *, kwonly1=None, kwonly1=None):
...
All named parameters after ``*`` must be a keyword::
fn(10, 'kw1', 'kw2') # Raises exception.
fn(10, kwonly1='kw1') # Ok.
Example
^^^^^^^
To define a function like above, do::
@positional(1)
def fn(pos1, kwonly1=None, kwonly2=None):
...
If no default value is provided to a keyword argument, it becomes a
required keyword argument::
@positional(0)
def fn(required_kw):
...
This must be called with the keyword parameter::
fn() # Raises exception.
fn(10) # Raises exception.
fn(required_kw=10) # Ok.
When defining instance or class methods always remember to account for
``self`` and ``cls``::
class MyClass(object):
@positional(2)
def my_method(self, pos1, kwonly1=None):
...
@classmethod
@positional(2)
def my_method(cls, pos1, kwonly1=None):
...
The positional decorator behavior is controlled by
``_helpers.positional_parameters_enforcement``, which may be set to
``POSITIONAL_EXCEPTION``, ``POSITIONAL_WARNING`` or
``POSITIONAL_IGNORE`` to raise an exception, log a warning, or do
nothing, respectively, if a declaration is violated.
Args:
max_positional_arguments: Maximum number of positional arguments. All
parameters after the this index must be
keyword only.
Returns:
A decorator that prevents using arguments after max_positional_args
from being used as positional parameters.
Raises:
TypeError: if a key-word only argument is provided as a positional
parameter, but only if
_helpers.positional_parameters_enforcement is set to

Closing as this works as intended.

@busunkim96 busunkim96 self-assigned this Feb 20, 2020
@busunkim96 busunkim96 added type: question Request for information or clarification. Not an issue. and removed triage me I really want to be triaged. labels Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants