KEMBAR78
Using os.PathLike as an annotation · Issue #5667 · python/mypy · GitHub
Skip to content

Using os.PathLike as an annotation #5667

@scolby33

Description

@scolby33

I am trying to create a typealias for all the types that can be passed as the first argument of open(). MyPy reveals the type to be Union[builtins.str, builtins.bytes, builtins.int, builtins._PathLike[Any]].

My first attempt at this is:

Openable = Union[str, bytes, int, os.PathLike[Any]]

MyPy is happy with this, but alas Python is not:

TypeError: 'ABCMeta' object is not subscriptable

Trying this, then:

Openable = Union[str, bytes, int, os.PathLike]

But now MyPy is unhappy:

error: Missing type parameters for generic type

What is the correct way of using os.PathLike here? There is no typing.PathLike yet. Do I have to create my own generic version of os.PathLike?

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions