Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appropriate Python error type for argument type mismatches

I am looking for the equivalent of PHPs InvalidArgumentException to use in case a function gets an argument that has an incorrect type. I'm working with Python 3. (And I am quite new to Python.)

ValueError documentation states "argument that has the right type but an inappropriate value", so is not the right one to use here.

TypeError documentation states "Raised when an operation or function is applied to an object of inappropriate type", and thus also does not seem applicable.

What is the correct error type to use in my case?

like image 604
Jeroen De Dauw Avatar asked Jun 14 '26 11:06

Jeroen De Dauw


1 Answers

I would go with TypeError in your case.

A function is applied to an object of inappropriate type can be understood as passing an argument of the wrong type (the function is applied to its arguments).

For instance, len(42) raises TypeError, because len() cannot be applied to a number.

like image 186
Frédéric Hamidi Avatar answered Jun 17 '26 02:06

Frédéric Hamidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!