Python 3 type hinting for None?

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):
    ...

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)