Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve --update-data handler #15283

Merged
merged 3 commits into from
May 31, 2023
Merged

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented May 22, 2023

Fixes #15265.

The new implementation works for two kinds of 'check' tests:

  • Test cases whose assertions are in [out] and/or [outN] sections, have those sections rewritten with the new results
  • Test cases whose assertions are inlined as Python comments in [case ...] and [file ...] sections, have the actual results inlined into source code

The two scenarios are mutually exclusive: if [out] is updated, then [case ...] won't be.

Details:

  • The "fixes" are enqueued and applied in reverse order to prevent line offsets from shifting as it incrementally changes the file.

  • The update preserves spacing between the source code and comment to reduce diff noise.

  • Like the previous implementation, we only update "check" data tests.

@ikonst ikonst force-pushed the update_testcase_output branch 2 times, most recently from ea590fb to 9f2425a Compare May 22, 2023 15:27
@ikonst ikonst marked this pull request as draft May 22, 2023 16:37
@ikonst ikonst marked this pull request as ready for review May 22, 2023 18:05
mypy/test/data.py Outdated Show resolved Hide resolved
mypy/test/testcheck.py Outdated Show resolved Hide resolved
mypy/test/data.py Outdated Show resolved Hide resolved
@github-actions

This comment has been minimized.

1 similar comment
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ikonst
Copy link
Contributor Author

ikonst commented May 25, 2023

@JelleZijlstra @hauntsaninja Finally (a) merged all those branch-out PRs, and (b) squashed that Windows bug in my test, so it's ready for review

Comment on lines +752 to +754
# start from end to prevent line offsets from shifting as we update
for fix in sorted(self._fixes, reverse=True):
lines[fix.lineno - 1 : fix.end_lineno - 1] = fix.lines
Copy link
Contributor Author

@ikonst ikonst May 30, 2023

Choose a reason for hiding this comment

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

A different approach here could have me parse the file, then re-write it while substituting specific sections (i.e. self._fixes would be a mapping of sections to lines), but I think this approach is simple enough.

@ikonst
Copy link
Contributor Author

ikonst commented May 30, 2023

@hauntsaninja ping

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks, this is awesome! Tried it out and it worked great on check tests.

@hauntsaninja hauntsaninja merged commit 3e03484 into python:master May 31, 2023
@ikonst ikonst deleted the update_testcase_output branch May 31, 2023 14:34
@hauntsaninja
Copy link
Collaborator

Was playing around with this a little more, I think it should probably ignore xfail tests?

@ikonst
Copy link
Contributor Author

ikonst commented Jun 1, 2023

Does it end up changing xfail'd tests? Hmpf. Yes, ignoring is the right thing to do (#15337).

@cdce8p
Copy link
Collaborator

cdce8p commented Jun 6, 2023

This PR seems to cause a failure in the wheel build. Strangely enough only with x86_64, arm64 is fine. I wasn't able to reproduce the failure unfortunately and CI tests are green as well. Furthermore, the error message seems to be different depending on the platform.
https://github.com/mypyc/mypy_mypyc-wheels/actions/runs/5129904681/jobs/9228043953#step:4:7141 (Linux)
https://github.com/mypyc/mypy_mypyc-wheels/actions/runs/5129904681/jobs/9228044912#step:4:6181 (MacOS)

@ikonst
Copy link
Contributor Author

ikonst commented Jun 6, 2023

I think it's a cwd problem, see #15383. The errors across macos and linux appear to be the same.

stdout

  ----------------------------- Captured stdout call -----------------------------
  ============================= test session starts ==============================
  platform linux -- Python 3.11.0rc2, pytest-7.3.1, pluggy-1.0.0
  rootdir: /tmp/tmp.mgLNGp/venv/lib/python3.11
  configfile: pytest.ini
  plugins: forked-1.6.0, xdist-3.3.1, cov-4.1.0
  collected 0 items
  ============================ no tests ran in 0.01s =============================
  `/tmp/tmp.mgLNGp/venv/bin/python -m pytest -n 0 -s --update-data mypy/test/testcheck.py::TypeCheckSuite::check-update-data.test` returned 4: 2 attempts remaining
  ============================= test session starts ==============================
  platform linux -- Python 3.11.0rc2, pytest-7.3.1, pluggy-1.0.0
  rootdir: /tmp/tmp.mgLNGp/venv/lib/python3.11
  configfile: pytest.ini
  plugins: forked-1.6.0, xdist-3.3.1, cov-4.1.0
  collected 0 items
  
  ============================ no tests ran in 0.01s =============================
  `/tmp/tmp.mgLNGp/venv/bin/python -m pytest -n 0 -s --update-data mypy/test/testcheck.py::TypeCheckSuite::check-update-data.test` returned 4: 1 attempts remaining

stderr

  ----------------------------- Captured stderr call -----------------------------
  ERROR: file or directory not found: mypy/test/testcheck.py::TypeCheckSuite::check-update-data.test

JelleZijlstra pushed a commit that referenced this pull request Jun 6, 2023
This should allow the inner pytest to pass even when the outer pytest is
invoked outside of the mypy root, e.g.
```shell
cd ..
pytest mypy/mypy/test/testupdatedata.py
```

Addresses
#15283 (comment).
@A5rocks
Copy link
Contributor

A5rocks commented Jul 12, 2023

I was playing around with this and I think it breaks on multiple errors? I'm getting test case headers overwritten! Might just be a user error though.

@ikonst
Copy link
Contributor Author

ikonst commented Jul 12, 2023

@A5rocks Try running pytest -n 0. I think it can happen in parallel invocations (the offsets determined at test time become wrong by update time).

Easiest improvement would be probably to either error out when --update-data is used with parallelism, or make it disable parallelism.

Another option would be to associate changes with identifiers ("update section Y of testcase X to Z"), then parse the file and replay them. Right now the update stage is simple/naive since at that point it's just replacing ranges of lines, not parsing any "INI" structure. (And, of course, we'd still need to hold a lock during the update.)

@A5rocks
Copy link
Contributor

A5rocks commented Jul 12, 2023

That's probably it. Thanks!

JelleZijlstra pushed a commit that referenced this pull request Jul 12, 2023
Fixes a
[gotcha](#15283 (comment))
with `--update-data`.

When using `--update-data` with parallelized tests (xdist), the line
offsets determined at test time may become wrong by update time, as
multiple workers perform their updates to the same file.

This makes it so we exit with a usage error when `--update-data` is used
with parallelism.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve --update-data
4 participants