Conversation
for more information, see https://pre-commit.ci
|
Not sure if this deserves a release note or any I'll take a look at Windows' fails tomorrow 👍 |
Took a bit longer but I'm getting @econchick Could you approve the workflow? 😄 |
|
Have been here myself. Getting the paths to work on all platforms and only being able to test thru a gh workflow. Here is a module that is specifically for the challenge this PR is about https://github.com/msftcangoblowm/wreck/blob/master/src/wreck/_safe_path.py If you hit a brick wall or feel going around in circles, this code might be helpful |
Paebbels
left a comment
There was a problem hiding this comment.
I like that transition to pathlib, but I found many mistakes and code parts that can be improved. See my list of suggestions.
I found this PR by accident. I'm no contributor to this repository. I'm just an excessive Python user.
| buf = [] | ||
| for fl in filenames: | ||
| with codecs.open(os.path.join(HERE, fl), "rb", encoding) as f: | ||
| with codecs.open(HERE / fl, "rb", encoding) as f: |
There was a problem hiding this comment.
| with codecs.open(HERE / fl, "rb", encoding) as f: | |
| with (HERE / fl).open("r", encoding) as f: |
You should open the file in text mode, not binary mode and then apply the encoding. Path objects provide a Path.open() method.
Since when is the abbreviation of filename fl?
| here = Path(__file__).parent.absolute() | ||
| with codecs.open(str(here.joinpath(*parts)), "rb", "utf-8") as f: |
There was a problem hiding this comment.
| here = Path(__file__).parent.absolute() | |
| with codecs.open(str(here.joinpath(*parts)), "rb", "utf-8") as f: | |
| HERE = Path(__file__).parent.absolute() | |
| with HERE.joinpath(*parts).open("r", encoding="utf-8") as f: |
No need to convert to a str, almost every open function/method accepts path-like objects. Don't open a file in binary when you apply an encoding.
Other parts use HERE.
| # need to write the badge as an svg first in order to convert it to | ||
| # another format | ||
| tmp_output_file = f"{os.path.splitext(output)[0]}.tmp.svg" | ||
| tmp_output_file = Path(f"{output.parent / output.stem}.tmp.svg") |
There was a problem hiding this comment.
| tmp_output_file = Path(f"{output.parent / output.stem}.tmp.svg") | |
| tmp_output_file = output.with_suffix(".tmp.svg") |
Source: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.with_suffix
|
|
||
| try: | ||
| badge = minidom.parse(output) | ||
| badge = minidom.parse(str(output)) |
There was a problem hiding this comment.
| badge = minidom.parse(str(output)) | |
| badge = minidom.parse(output) |
minidom accepts file-like objects: https://docs.python.org/3/library/xml.dom.minidom.html#xml.dom.minidom.parse
| """ | ||
| with open(path_config, "rb") as f: | ||
| pyproject_toml = tomllib.load(f) | ||
| pyproject_toml = tomllib.loads(Path(path_config).read_text()) |
There was a problem hiding this comment.
| pyproject_toml = tomllib.loads(Path(path_config).read_text()) | |
| pyproject_toml = tomllib.loads(Path(path_config).read_text(encoding="utf-8")) |
No encoding is specified.
|
|
||
| expected_output = expected_output.replace("\n", "") | ||
| expected_output_path = FIXTURES / "expected_badge.svg" | ||
| expected_output = expected_output_path.read_text().replace("\n", "") |
There was a problem hiding this comment.
| expected_output = expected_output_path.read_text().replace("\n", "") | |
| expected_output = expected_output_path.read_text(encoding="utf-8").replace("\n", "") |
| with open(expected_fixture) as f: | ||
| expected_out = f.read() | ||
| expected_fixture = FIXTURES / "windows" / exp_fixture_file | ||
| expected_out = expected_fixture.read_text() |
There was a problem hiding this comment.
| expected_out = expected_fixture.read_text() | |
| expected_out = expected_fixture.read_text(encoding="utf-8") |
|
|
||
| with open(expected_fixture) as f: | ||
| expected_out = f.read() | ||
| expected_out = expected_fixture.read_text() |
There was a problem hiding this comment.
| expected_out = expected_fixture.read_text() | |
| expected_out = expected_fixture.read_text(encoding="utf-8") |
| expected = f.read() | ||
| expected = expected.replace("\n", "").replace("\r", "") | ||
| expected_fixture = FIXTURES / "default-style" / "99.svg" | ||
| expected = expected_fixture.read_text().replace("\n", "").replace("\r", "") |
There was a problem hiding this comment.
| expected = expected_fixture.read_text().replace("\n", "").replace("\r", "") | |
| expected = expected_fixture.read_text(encoding="utf-8").replace("\n", "").replace("\r", "") |
| expected_contents = ( | ||
| expected_fixture.read_bytes() | ||
| if out_format == "png" | ||
| else expected_fixture.read_text() |
There was a problem hiding this comment.
| else expected_fixture.read_text() | |
| else expected_fixture.read_text(encoding="utf-8") |
Thanks, a lot of these are good suggestions! Not sure what's up with the snark though (e.g. "Since when is the abbreviation of filename I'll probably hold off on applying these until there's some interest from Lynn in this PR actually going somewhere. |
|
@trag1c The comment about |
Hey, I just made a Pull Request!
Description
Changes all occurrences of
os.pathto usepathlibinstead.Motivation and Context
Closes #179
Have you tested this? If so, how?
Updated unit tests :)
Checklist for PR author(s)
tox -e docsorinterrogate -c pyproject.toml(I mean, we should set a good example 😄).README.rst.README.rstanddocs/index.rstfor any new/changed CLI flags.versionadded,versionchanged, ordeprecateddirectives. Find the appropriate next version in the project's__init__.pyfile.Release note