Skip to content

Conversation

@d-chris
Copy link
Contributor

@d-chris d-chris commented Mar 18, 2025

fixes #283

introduced option to specify file encoding when overwriting files.

default values

  • ini-file: None
  • cli-option: "utf-8"

ini-file always overwrite command line option

command line option

--text-file-encoding=TEXT_FILE_ENCODING
                        Specify encoding for files.

configuration file

  text_file_encoding (string):
                        Default encoding for text files.

Copy link
Contributor

@pllim pllim left a comment

Choose a reason for hiding this comment

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

Thanks!

More eyes on review would be good. The diff looks reasonable to me.



def write_modified_file(fname, new_fname, changes):
def write_modified_file(fname, new_fname, changes, encoding=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the default here be consistent with addoption default (utf-8)?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it should be consistent, though I'm not sure if we should change the default to utf-8 or leave as is now?

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 decided to set encoding=None to avoid changing the behavior of the write_modified_file function since I am not familiar with your codebase. I wanted to ensure that the function behaves as it did before.

From a developer's perspective, I would prefer using "utf-8" as the default encoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

any changes required?

i guess the default value of write_modified_file is uncritical.

default value for cli option defaults to "utf-8" which will be passed on.

@bsipocz bsipocz modified the milestones: v1.4.0, v1.5.0 Mar 18, 2025
@bsipocz
Copy link
Member

bsipocz commented Mar 18, 2025

@pllim - bugfix or enhancement?

Copy link
Member

@bsipocz bsipocz left a comment

Choose a reason for hiding this comment

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

Thank you! The diff looks good to me. However, this will definitely need a test, the one in the bug report issue would be great. And will eventually need an entry in the changelog, though we need to agree on if this is a feature or a bugfix first.

@pllim
Copy link
Contributor

pllim commented Mar 18, 2025

bugfix or enhancement?

Hmm... It does introduce a new option, so more like enhancement?

@d-chris d-chris requested a review from bsipocz March 18, 2025 22:07
@d-chris
Copy link
Contributor Author

d-chris commented Mar 19, 2025

i tried to implement some tests

but seems with with testdir.inline_run(p, "--doctest-plus-generate-diff=diff", "--text-file-encoding", encoding) that the DebugRunnerPlus has an issue.

Traceback (most recent call last):
  File "C:\Python313\Lib\doctest.py", line 1395, in __run
    exec(compile(example.source, filename, "single",
         ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 compileflags, True), test.globs)
                 ^^^^^^^^^^^^^^^^^^^
  File "<doctest test_file_encoding_utf8.f[0]>", line 1
    print(☆)
          ^
SyntaxError: invalid character '☆' (U+2606)`

see commit f91f597

@d-chris d-chris marked this pull request as draft March 19, 2025 10:22
@d-chris
Copy link
Contributor Author

d-chris commented Mar 19, 2025

changed PR to Draft

todos

  • fix issue with syntax error
  • refactore my pytest functions, split into seperate tests for diff and overwrite
  • write a small contribution.md / requirements-dev.txt, how i set up .venv to develop for windows

@bsipocz
Copy link
Member

bsipocz commented Mar 19, 2025

@d-chris - the last todo point should definitely not be mixed into this PR

@d-chris d-chris marked this pull request as ready for review March 23, 2025 12:01
@d-chris
Copy link
Contributor Author

d-chris commented Mar 23, 2025

for testing implemented alternative fixture to generate temporary python files, due issue #285

@d-chris
Copy link
Contributor Author

d-chris commented Mar 24, 2025

somehow a can't trigger action within my fork, any tips?

i created a new action (outside of the PR scope) -> pass https://github.com/d-chris/pytest-doctestplus/actions/runs/14041267921

so the small fix from 09979a3 solved the issue

Copy link
Contributor Author

@d-chris d-chris left a comment

Choose a reason for hiding this comment

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

fixed in 09979a3

@pllim
Copy link
Contributor

pllim commented Mar 24, 2025

Github Actions has to be enabled explicitly by maintainer if it detects a first time contributor. It is how they attempt to prevent bitcoin miners. Nothing you could have done on your side. I just enabled the CI. Thanks for your patience!

@d-chris
Copy link
Contributor Author

d-chris commented Mar 24, 2025

Thanks for your patience—this is my first GH contribution ever!

Is there anything else I need to do to finish the PR?

@pllim
Copy link
Contributor

pllim commented Mar 24, 2025

Since @bsipocz requested changes, would be nice if she could re-review. 🙏



def write_modified_file(fname, new_fname, changes):
def write_modified_file(fname, new_fname, changes, encoding=None):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

any changes required?

i guess the default value of write_modified_file is uncritical.

default value for cli option defaults to "utf-8" which will be passed on.

@bsipocz
Copy link
Member

bsipocz commented Mar 24, 2025

Since @bsipocz requested changes, would be nice if she could re-review

Yeap, I plan to come back to this and the other PR later this week.

Copy link
Member

@bsipocz bsipocz left a comment

Choose a reason for hiding this comment

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

OK, this looks good to me. My only question is if we really need to add a new config option or reusing the existing doctest_encoding would be sufficient. Have you looked into that option? Obviously it only affects the adding a new parser option and not the actual code fixes where you added the usage of encoding.

Copy link
Member

@bsipocz bsipocz left a comment

Choose a reason for hiding this comment

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

Oh, and this will need a changelog entry, but I can deal with that at release time.

@d-chris d-chris marked this pull request as draft April 1, 2025 08:06
@d-chris d-chris marked this pull request as ready for review April 1, 2025 14:13
@bsipocz
Copy link
Member

bsipocz commented Apr 1, 2025

Hmm, failures look relevant for pytest < 7.4, but I haven't looked into the details.

Possible options:

  • find an easy workaround that makes this feature work; or only have it for 7.4+
  • we can consider dropping support for older versions. We don't really follow SPEC0 for these infrastructure tools but keep support until we run into a blockage, and I would be open to discuss if this is a blockage (7.3 will just about pass the 2 year limit in a couple of days)

@d-chris
Copy link
Contributor Author

d-chris commented Apr 1, 2025

i use option --config-file to specify the ini file while running my tests.

whats confuses me i always run tox before a review, and i always pass all tests

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --config-file /tmp/pytest-of-runner/pytest-0/test_basic_file_encoding_overwrite3/pytest.ini

i guess there i just have to rewrite my tests to pass the configuration file in the correct way. dropping support should not be really necessary?

@bsipocz
Copy link
Member

bsipocz commented Apr 1, 2025

ahh, indeeed. --config-file was new in 7.4: https://github.com/pytest-dev/pytest/releases/tag/7.4.0

@d-chris
Copy link
Contributor Author

d-chris commented Apr 1, 2025

-c should do the trick

pytest --version & pytest --help | findstr "config"
This is pytest version 5.1.0, imported from D:\temp\pytest5\.venv\lib\site-packages\pytest.py
                        configuration file raise errors.
  -c file               load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
test session debugging and configuration:
  -h, --help            show help message and configuration info
  --trace-config        trace considerations of conftest.py files.

Copy link
Member

@bsipocz bsipocz left a comment

Choose a reason for hiding this comment

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

This all looks good now. Thank you @d-chris for your contribution and patience during the review process.

@bsipocz bsipocz merged commit 9021ee3 into scientific-python:main Apr 11, 2025
19 checks passed
@d-chris d-chris deleted the d-chris/issue283 branch April 11, 2025 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytest --doctest-plus-generate-diff=overwrite crashes on windows with utf-8 files

3 participants