Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: required kwarg, which exception to raise?

One way to ensure that a method is called with a particular kwarg would be like:

def mymethod(self, *args, **kwargs):     assert "required_field" in kwargs 

Raising an AssertionError doesn't seem like the most appropriate thing to do. Is there an agreed upon builtin exception to handle this with a nice error message?

More info: there are 3rd-party subclassing issues where *args and **kwargs kinda' need to be passed so making "required_field" a positional argument is not really a good option.

like image 382
Skylar Saveland Avatar asked Nov 16 '12 18:11

Skylar Saveland


People also ask

How do you raise an exception in Python?

As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.

What is a Kwarg in Python?

**kwargs allows us to pass a variable number of keyword arguments to a Python function. In the function, we use the double-asterisk ( ** ) before the parameter name to denote this type of argument.

How do I beat Kwarg?

Precede double stars ( ** ) to a dictionary argument to pass it to **kwargs parameter. Always place the **kwargs parameter at the end of the parameter list, or you'll get an error.

Can you have Kwargs without args?

Python is pretty flexible in terms of how arguments are passed to a function. The *args and **kwargs make it easier and cleaner to handle arguments. The important parts are “*” and “**”. You can use any word instead of args and kwargs but it is the common practice to use the words args and kwargs.

How does Python know when an exception has been raised?

Note that you don’t need to specify the exception object in the raise statement. In this case, Python knows that the raise statement will raise the current exception that has been caught by the except clause. And you’ll see both the logging message and the exception in the output:

What is the difference between *ARGs and kwargs in Python?

The names `args` and `kwargs` are used by convention, they are not a part of the language specification. Thus, these are equivalent: If any positional argument follow `*args`, they are keyword-only arguments that can only be passed by name.

What is the base class for exceptions in Python?

Note: Exception is the base class for all the exceptions in Python. You can check the exception hierarchy here . Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.

How do I throw an exception in Python?

As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.


2 Answers

>>> def foo(bar): pass ...  >>> foo() Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: foo() missing 1 required positional argument: 'bar' 

I'd just go with TypeError..

like image 120
Otto Allmendinger Avatar answered Oct 02 '22 02:10

Otto Allmendinger


+1 for TypeError. This is what Python 3 raises for required keyword-only arguments:

>>> def foo(*, x): ...     pass ...  >>> foo() Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: foo() needs keyword-only argument x >>> foo(2) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: foo() takes exactly 0 positional arguments (1 given) >>> foo(x=2) 

(TypeError suggestion was already given (and accepted); I wrote this answer to mention this Python 3 feature).

like image 44
Mikhail Korobov Avatar answered Oct 02 '22 02:10

Mikhail Korobov