Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 type hinting for None?

def foo(
        hello: str='world', bar: str=None,
        another_string_or_None: str|????=None):
    pass

I'm trying to set a type hint in Python in a function, you can add more than one type hint with something: str|bool='default value', but, what are the type hinting for None? :/

like image 782
fj123x Avatar asked Oct 05 '13 20:10

fj123x


People also ask

How do you type hinting in Python?

Here's how you can add type hints to our function: Add a colon and a data type after each function parameter. Add an arrow ( -> ) and a data type after the function to specify the return data type.

Should I use type hinting in Python?

Type hints work best in modern Pythons. Annotations were introduced in Python 3.0, and it's possible to use type comments in Python 2.7. Still, improvements like variable annotations and postponed evaluation of type hints mean that you'll have a better experience doing type checks using Python 3.6 or even Python 3.7.

Does Python enforce type hints?

Unlike how types work in most other statically typed languages, type hints by themselves don't cause Python to enforce types. As the name says, type hints just suggest types. There are other tools, which you'll see later, that perform static type checking using type hints.

What does type () do in Python?

Syntax of the Python type() function The type() function is used to get the type of an object. When a single argument is passed to the type() function, it returns the type of the object. Its value is the same as the object.


4 Answers

From your example:

def foo(
        hello: str='world', bar: str=None,
        another_string_or_None: str|????=None):
    ...

I've noticed that your use case is "something or None".

Since version 3.5, Python supports type annotations via typing module. And in your case, the recommended way of annotating is by using typing.Optional[something] hint. This has exact meaning you're looking for.

Therefore the hint for another_string_or_None would be:

import typing

def foo(
        hello: str='world', bar: str=None,
        another_string_or_None: typing.Optional[str]=None):
    ...
like image 67
mbdevpl Avatar answered Oct 18 '22 03:10

mbdevpl


It's just None!

>>> def nothing(nun: None) -> None:
...     return nun
... 
>>> nothing(None)
>>> 

Or at least, it can be.

Since these annotations are meaningless to Python beyond being in/correct syntax, it's sort of up to the tools.

If you use typecheck-decorator for example, then you'll need to use type(None):

>>> import typecheck as tc
>>>
>>> @tc.typecheck
>>> def nothing(nun: type(None)) -> type(None):
...     return nun
... 
>>> nothing(None)
>>> nothing(0)
typecheck.framework.InputParameterError: nothing() has got an incompatible value for nun: 0
>>> nothing(False)
typecheck.framework.InputParameterError: nothing() has got an incompatible value for nun: False

Typecheck also allows you to somewhat more clearly "add more than one type hint with" with tc.any() (OR), tc.all() (AND), and far more besides.

Beware that tc.none() is a NAND-like predicate; not what you are looking for - with no arguments it will accept any type, equivalent to tc.all() or the more apt tc.anything.

like image 22
OJFord Avatar answered Oct 18 '22 04:10

OJFord


Python 3.10 will support your original desired notation: str | None.

Source

like image 29
Oddthinking Avatar answered Oct 18 '22 05:10

Oddthinking


I know this question is considered answered thanks to @mbdevpl, however, I've wanted to add that type(None) is how you get the actual for None type, this can be useful for example in an if statement check like:

if isinstance(x_var, type(None)):
    pass

and since python3.5, you can also use do Union of a bunch of types with None as shown here:

x_var: typing.Union[str, None]
y_var: typing.Union[Dict, List, None]

this is equivalent to:

x_var: typing.Optional[str]
y_var: typing.Optional[typing.Union[Dict, List]]
like image 13
Hocine Abdellatif Houari Avatar answered Oct 18 '22 03:10

Hocine Abdellatif Houari