top | item 45400278

(no title)

letmeinhere | 5 months ago

Yeah post 3.10 you don't need Union, Optional, List, Duct, Tuple. Any still necessary when you want to be permissive, and I'm still hoping for an Unknown someday...

discuss

order

maleldil|5 months ago

> hoping for an Unknown someday

Wouldn't that just be `object` in Python?

lexicality|5 months ago

No, because the type checker should prevent you interacting with `Unknown` until you tie it down, but `object` is technically a valid type

Narushia|5 months ago

That's what I use it for. If you type something as `object`, static type checkers can just narrow down the exact typing later.

__MatrixMan__|5 months ago

> List, Duct, Tuple...

I'm aware this is just a typo but since a lot of the Python I write is in connection with Airflow I'm now in search of a way to embrace duct typing.

rgovostes|5 months ago

I am glad they improved this but I still like Optional[], and to a lesser extent, Union[]. It's much more readable to have Optional[str] compared to str | None.

Narushia|5 months ago

I disagree with `Optional`. It can cause confusion in function signatures, since an argument typed as "optional" might still be required if there is no default value. Basically I think the name is bad, it should be `Nullable` or something.

I believe Python's own documentation also recommends the shorthand syntax over `Union`. Linters like Pylint and Ruff also warn if you use the imported `Union`/`Optional` types. The latter even auto-fixes it for you by switching to the shorthand syntax.