Skip to content

Detect improvable code with open + pathlib.Path #5289

@kasium

Description

@kasium

Current problem

In the python world. developers tend to use both the old os.path based system and pathlib in parallel. This can lead to some confusion.
Recently I've seen some code like this:

from pathlib import Path
path = Path("foo")
with open(str(path)) as handle:
  pass

Desired solution

Since open can consume a Path object, the above code can be improved to open(path) or path.open()

Additional context

I'm not 100% sure if this is in the scope of pylint, but calling str over and over again will lead to a worse performance (even if not too much). Therefore I see such a check as a benefit.
I'm also happy to open a PR, if the idea is accepted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentNeeds PRThis issue is accepted, sufficiently specified and now needs an implementationProposal 📨checker-CodeStyleIssues and PRs that relate to the `CodeStyleChecker`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions