Python 3.10 Type Hinting with Union and Optional Causing advanced patterns
I'm stuck trying to I've been banging my head against this for hours. I'm dealing with Hey everyone, I'm running into an issue that's driving me crazy. I'm working on a project and hit a roadblock. I'm relatively new to this, so bear with me. I'm working with an unexpected behavior while using type hinting in Python 3.10. I have a function that is supposed to accept either an integer or None, and I used `Union[int, None]` to specify this. However, when I try to call this function with a NoneType, it raises a TypeError rather than accepting it as I expected. Here's my function: ```python from typing import Union def process_value(value: Union[int, None]) -> str: if value is None: return 'Value is None' elif isinstance(value, int): return f'Value is: {value}' else: raise TypeError('Invalid type') ``` When I call the function like this: ```python result = process_value(None) print(result) ``` I get the following behavior: ``` TypeError: Invalid type ``` I also tried using `Optional[int]`, but that didn't yield different results. Hereβs how I defined it: ```python from typing import Optional def process_value(value: Optional[int]) -> str: # same code as above ``` I would expect the output to be 'Value is None'. Is there something I am missing about type hints or how they interact with None? Any help would be appreciated! My development environment is macOS. Any help would be greatly appreciated! For context: I'm using Python on Ubuntu. Any help would be greatly appreciated! For context: I'm using Python on Windows. Any examples would be super helpful. I've been using Python for about a year now. What am I doing wrong? The project is a web app built with Python.