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):
...
Answer from mbdevpl on Stack OverflowFrom 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):
...
Python 3.10 will support your original desired notation: str | None.
Source
Videos
So, I wanted to get a general idea about how people feel about giving return type hint of None for a function that doesn't return anything.
With the introduction of PEP 484, type hints were introduced and we all rejoiced. Lot of my coworkers just don't get the importance of type hints and I worked way too hard to get everyone onboarded so they can see how incredibly useful it is! After some time I met a coworker who is a fan of typing and use it well... except they write -> None everywhere!
Now this might be my personal opinion, but I hate this because it's redundant and not to mention ugly (at least to me). It is implicit and by default, functions return None in python, and I just don't see why -> None should be used. We have been arguing a lot over this since we are building a style guide for the team and I wanted to understand what the general consensus is about this. Even in PEP 484, they have mentioned that -> None should be used for __init__ functions and I just find that crazy.
Am I in the wrong here? Is this fight pointless? What are your opinions on the matter?
Sorry for the confusing title.
I have been adding some type hinting into my code and noticed that if I have something like:
def create(self, a: int = None) -> None a = a or self.a
Where I would like to have the user either give a as an int or just skip it and the function will default to the class' initial value for a. This works just fine but this shows in the IDE as create(self, a: int | None = None) -> None which looks like None is a valid value for a, even though that is not what I am looking for.
Then if I have an artificial case like:
def create(self, a: int = 'test') -> None ...
Which then shows up as create(self, a: int = 'test') -> None which looks weird but at least it doesn't explicitly imply that str is a valid parameter.
So I guess two questions: what makes None special? And, is this the way to use type hinting when I need to default to class' initial value or get an argument from user?
Recently, one of my coworkers and I had a long debate on using Union[str, None] vs Optional[str]. I wrote a function that was supposed to return either a string or None. Based on the returned value my program was supposed to trigger some action. In my opinion, since None was a deciding factor it would be a better choice to use Union[str, None] for the sake of clarity. However, according to his opinion, Optional is a better choice in terms of simplicity/cleaner look. We both understand clearly that Optional is nothing but a shorthand of Union[..., None]. After a few searches, we figured people use both and both teams have their logic. In fact, the creator of Fast API, u/tiangolo also supports using Union (According to one of his recent tweets). My question is, what do you use and why?
The "modern" way to write this is str | None. Traditionally, I used Optional[str], mostly because Optional existed specifically for this purpose.
Assuming it's between these two, using the Union basic says Optional exists but should never be used.
I'm on team Optional here, that's exactly the use case for it so if we don't use it in that case there's no point for it having been accepted.
Sure, use the new syntax if you have it but if you don't then prefer Optional over Union with None.
By the way, None isn't a type - Union with NoneType also makes sense and this is a quibble that doesn't come up with Optional. We all know what Optional means.
https://blog.jonathanchun.com/2025/02/16/to-type-or-not-to-type/
I wrote this blog post as I've seen a lot of newer developers complain about Type hints and how they seem unnecessary. I tried to copy-paste a short excerpt from the blog post here but it kept detecting it as a question which is not allowed, so decided to leave it out.
I know there's plenty of content on this topic, but IMO there's still way too much untyped Python code!