Skip to content

Conversation

kurtmckee
Copy link
Contributor

pyupgrade is a useful tool for upgrading old code styles to newer Python idioms.

This PR introduces pyupgrade as a pre-commit hook, runs pre-commit run -a twice, and commits the changes.

pyupgrade found the following classes of improvements:

  • typing.Callable, and several async typing types, are deprecated aliases for collections.abc.Callable et al.
  • open(file, "r") was replaced with open(file), which is equivalent.
  • %-style string formatting was replaced with f-strings.
  • Union[x, y] and Optional[x] can now be expressed as x | y and x | None.

The test suite passed locally after these changes, but I'll watch CI as well.

@okhat
Copy link
Collaborator

okhat commented Aug 14, 2025

Wow, typing.Callable is deprecated now..?

@kurtmckee
Copy link
Contributor Author

typing.Callable is deprecated now..?

Yep, since Python 3.9. I think the difference was that collections.abc.Callable didn't support type annotation subscripts initially, so it wasn't possible to annotate the callable's parameters and return value:

def f(function: collections.abc.Callable[[int], bool]) -> bool:
    return function(123)

typing.Callable was added to support this and backported via typing_extensions IIRC, but now collections.abc.Callable supports type annotation subscripts.

@kurtmckee
Copy link
Contributor Author

Rebased on main, resolved merge conflicts, and force-pushed.

@kurtmckee
Copy link
Contributor Author

Rebased on main, resolved merge conflicts again, and force-pushed.

@kurtmckee
Copy link
Contributor Author

@okhat would you be willing to take another look at this PR when you have an opportunity? It's okay to reject it, I'm just hoping to avoid the need to resolve merge conflicts with main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants