Skip to content

Comments

Remove python 2 compatibility, add type hints#80

Merged
xuhdev merged 8 commits intoeditorconfig:masterfrom
petamas:add-type-hints
Jun 7, 2025
Merged

Remove python 2 compatibility, add type hints#80
xuhdev merged 8 commits intoeditorconfig:masterfrom
petamas:add-type-hints

Conversation

@petamas
Copy link
Contributor

@petamas petamas commented May 26, 2025

Resolves #78

This is my first time using Travis, so I hope it'll work as I think it works.

@petamas
Copy link
Contributor Author

petamas commented May 26, 2025

Sorry for the force-push, I've accidentally opened the PR from an earlier version of my branch than I intended.

Copy link
Member

@xuhdev xuhdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Please add my check to a new GitHub workflow since you added type annotations. Alternatively you can separate type annotations to a separate PR.

@petamas
Copy link
Contributor Author

petamas commented May 27, 2025

@xuhdev : Just to make sure I understand correctly: you want me to remove travis.yml, and instead create a new workflow in .github/workflows to run mypy, right? I.e. do not simply extend runtime.yml?

@petamas petamas force-pushed the add-type-hints branch 3 times, most recently from b780749 to 9c44818 Compare May 27, 2025 09:30
split_version[3] = "final"
split_version = list(map(int, split_version[:3])) + split_version[3:]
return tuple(split_version)
return (int(split_version[0]), int(split_version[1]), int(split_version[2]), split_version[3])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old code looks fine.

Suggested change
return (int(split_version[0]), int(split_version[1]), int(split_version[2]), split_version[3])
return tuple(list(map(int, split_version[:3])) + split_version[3:])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy doesn't understand the old code, that's why I changed it. (Also, I think it's much more readable after the change, but that's just my opinion.)

"""

import re
from typing import Optional, Tuple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional is still needed on 3.9, but Tuple can be written as tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right, I was targeting 3.8 when I wrote it, but with 3.9, it's unnecessary . I'll update all Tuples to tuple and Lists to list.

@petamas
Copy link
Contributor Author

petamas commented May 28, 2025

@greut: I've addressed the Tuple/tuple (and List/list, and typing.Pattern/re.pattern) issues.

I also tried the old version of the split_version code, but mypy's not smart enough to understand it:

editorconfig\versiontools.py:37: error: Incompatible return value type (got "tuple[str | int, ...]", expected "tuple[int, int, int, str] | None")  [return-value]

It successfully figures out that split_version is a list of ints and strings, but cannot assign indices to the types. While the new version is slightly more verbose, it is clearer for both mypy and human readers.

Copy link
Member

@xuhdev xuhdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than the comment. @greut may have more comments :)

test:
name: Static type checking
runs-on: ubuntu-latest
container: python:3.13-alpine3.21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want to use alpine3.21 here, renovate bots won't bump 3.21. Maybe just alpine or use setup-python?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That part was copied verbatim from the other workflow (runtime.yml).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to change it of course, just wanted to point out that sticking to a specific alpine version was not my own decision.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it's fine then. We can tackle it separately.

@xuhdev
Copy link
Member

xuhdev commented May 30, 2025

ping @greut , does this look good to you :)

@petamas petamas requested a review from greut June 4, 2025 14:40
@petamas
Copy link
Contributor Author

petamas commented Jun 4, 2025

@greut, does this look OK to you? I'd appreciate if this could be merged.

@xuhdev xuhdev merged commit d789e10 into editorconfig:master Jun 7, 2025
2 checks passed
@xuhdev
Copy link
Member

xuhdev commented Jun 7, 2025

Thanks for the contribution! I merged for now, @greut is always welcome to comment and we can do a followup.

@petamas petamas deleted the add-type-hints branch June 7, 2025 20:42
@petamas
Copy link
Contributor Author

petamas commented Jun 7, 2025

@xuhdev : Thanks!

Question: what's the timeline on the next release of the package that includes my changes?

@xuhdev
Copy link
Member

xuhdev commented Jun 7, 2025

We can make a bug fix release on demand. Let me know what you think; feel free to create a patch version bump PR. I can tag it :)

@petamas petamas mentioned this pull request Jun 8, 2025
@petamas
Copy link
Contributor Author

petamas commented Jun 8, 2025

@xuhdev: Thanks! Here's the PR: #82

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.

Supported Python versions?

3 participants