What is the correct way in python to annotate a path with type hints? [duplicate]

I assume that typical path objects are either Paths or strs, as such you could use a Union. In addition, the more inclusive os.PathLike is preferred over pathlib.Path.

Python 3.10 or newer:

import os

def read_json(path: str | os.PathLike):
    ...

Python 3.5 – 3.9:

import os
import typing

def read_json(path: typing.Union[str, os.PathLike]):
    ...

Leave a Comment

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