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

Switch to pytest asserts #1276

Closed
wants to merge 1 commit into from
Closed

Conversation

pquentin
Copy link
Member

@pquentin pquentin commented Jun 4, 2021

They're easier to read and produce better error messages. This was done mostly automatically with the self_assert unittest2pytest fix. (I had to modify unittest2pytest to use fissix instead of lib2to3 as the latter choked on various files.)

I also had to change many lines from assert 0 == value to assert value == 0 to please pytest. This isn't a useful pattern in Python because assert 0 = value is a syntax error anyway.

I may have some pylint issues left to fix, but wanted to hear about the general idea first.

They're easier to read and produce better error messages. This was done
mostly automatically with the `self_assert` unittest2pytest fix. (I had
to modify unittest2pytest to use fissix instead of lib2to3 as the latter
choked on various files.)

I also had to change many lines from `assert 0 == value` to `assert
value == 0` to please pytest. This isn't a useful pattern in Python
because `assert 0 = value` is a syntax error anyway.
@danielmitterdorfer danielmitterdorfer added :misc Changes that don't affect users directly: linter fixes, test improvements, etc. cleanup Linter changes, reformatting, removal of unused code etc. labels Jun 7, 2021
@danielmitterdorfer danielmitterdorfer self-requested a review June 7, 2021 07:37
@danielmitterdorfer
Copy link
Member

@elasticmachine test this please

@danielmitterdorfer danielmitterdorfer added this to the 2.2.1 milestone Jun 7, 2021
@pquentin
Copy link
Member Author

pquentin commented Jun 7, 2021

This kind of large transformation is much easier to do once automatic code formatting is in place, have you considered switching to black?

I also need to change the assertAlmostEqual transformation to use math.isclose, which is the idiomatic way to compare floats since Python 3.5. The current round/abs hacks are very difficult to understand.

I can also do this file by file or assert type by assert type to make this easier to review in small chunks.

Copy link
Member

@danielmitterdorfer danielmitterdorfer 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 your PR! IMHO the general direction makes sense.

I have triggered CI and as you suspected there are indeed there are a couple of linter issues at the moment:

************* Module tests.utils.opts_test
tests/utils/opts_test.py:37:0: C0301: Line too long (145/140) (line-too-long)
************* Module tests.mechanic.mechanic_test
tests/mechanic/mechanic_test.py:57:0: C0301: Line too long (150/140) (line-too-long)
************* Module tests.mechanic.team_test
tests/mechanic/team_test.py:155:0: C0301: Line too long (203/140) (line-too-long)
tests/utils/opts_test.py:37:0: C0301: Line too long (145/140) (line-too-long)
tests/mechanic/team_test.py:155:0: C0301: Line too long (203/140) (line-too-long)
************* Module tests.config_test
tests/config_test.py:293:0: C0301: Line too long (141/140) (line-too-long)
************* Module tests.track.params_test
tests/track/params_test.py:138:0: C0301: Line too long (153/140) (line-too-long)
tests/track/params_test.py:141:0: C0301: Line too long (143/140) (line-too-long)
tests/track/params_test.py:797:0: C0301: Line too long (143/140) (line-too-long)
tests/track/params_test.py:1373:0: C0301: Line too long (149/140) (line-too-long)
tests/track/params_test.py:1797:0: C0301: Line too long (142/140) (line-too-long)
tests/mechanic/mechanic_test.py:57:0: C0301: Line too long (150/140) (line-too-long)
************* Module tests.mechanic.supplier_test
tests/mechanic/supplier_test.py:34:0: C0301: Line too long (142/140) (line-too-long)
tests/mechanic/supplier_test.py:37:0: C0301: Line too long (207/140) (line-too-long)
tests/mechanic/supplier_test.py:177:0: C0301: Line too long (151/140) (line-too-long)
tests/mechanic/supplier_test.py:183:0: C0301: Line too long (151/140) (line-too-long)
tests/mechanic/supplier_test.py:542:0: C0301: Line too long (166/140) (line-too-long)
tests/mechanic/supplier_test.py:555:0: C0301: Line too long (164/140) (line-too-long)
************* Module tests.telemetry_test
tests/telemetry_test.py:291:0: C0301: Line too long (157/140) (line-too-long)
tests/telemetry_test.py:304:0: C0301: Line too long (150/140) (line-too-long)
tests/telemetry_test.py:1875:0: C0301: Line too long (158/140) (line-too-long)
tests/telemetry_test.py:2531:0: C0301: Line too long (163/140) (line-too-long)
tests/telemetry_test.py:2544:0: C0301: Line too long (159/140) (line-too-long)
************* Module tests.metrics_test
tests/metrics_test.py:190:0: C0301: Line too long (150/140) (line-too-long)
tests/metrics_test.py:201:0: C0301: Line too long (152/140) (line-too-long)
tests/metrics_test.py:213:0: C0301: Line too long (149/140) (line-too-long)
tests/metrics_test.py:226:0: C0301: Line too long (151/140) (line-too-long)
************* Module tests.track.loader_test
tests/track/loader_test.py:240:0: C0301: Line too long (150/140) (line-too-long)
tests/track/loader_test.py:1759:0: C0301: Line too long (144/140) (line-too-long)
tests/track/loader_test.py:1848:0: C0301: Line too long (145/140) (line-too-long)
tests/track/loader_test.py:1882:0: C0301: Line too long (146/140) (line-too-long)
tests/track/loader_test.py:1936:0: C0301: Line too long (149/140) (line-too-long)
tests/track/loader_test.py:1966:0: C0301: Line too long (153/140) (line-too-long)
tests/track/loader_test.py:2969:0: C0301: Line too long (153/140) (line-too-long)
tests/track/loader_test.py:3005:0: C0301: Line too long (149/140) (line-too-long)
tests/track/loader_test.py:3519:0: C0301: Line too long (152/140) (line-too-long)
tests/track/loader_test.py:3557:0: C0301: Line too long (151/140) (line-too-long)
************* Module tests.driver.runner_test
tests/driver/runner_test.py:192:0: C0301: Line too long (155/140) (line-too-long)
tests/driver/runner_test.py:2177:0: C0301: Line too long (151/140) (line-too-long)
tests/driver/runner_test.py:2193:0: C0301: Line too long (149/140) (line-too-long)
tests/driver/runner_test.py:2456:0: C0301: Line too long (154/140) (line-too-long)
tests/driver/runner_test.py:2673:0: C0301: Line too long (152/140) (line-too-long)
tests/driver/runner_test.py:2751:0: C0301: Line too long (152/140) (line-too-long)
tests/driver/runner_test.py:2796:0: C0301: Line too long (156/140) (line-too-long)
tests/driver/runner_test.py:2874:0: C0301: Line too long (156/140) (line-too-long)
tests/driver/runner_test.py:2920:0: C0301: Line too long (157/140) (line-too-long)
tests/driver/runner_test.py:2999:0: C0301: Line too long (157/140) (line-too-long)

You can also see them locally when running make lint.

@danielmitterdorfer
Copy link
Member

This kind of large transformation is much easier to do once automatic code formatting is in place, have you considered switching to black?

black is on our radar but I fear that we won't have much capacity at the moment to work on it.

I also need to change the assertAlmostEqual transformation to use math.isclose, which is the idiomatic way to compare floats since Python 3.5. The current round/abs hacks are very difficult to understand.

That's great. :)

I can also do this file by file or assert type by assert type to make this easier to review in small chunks.

Given the mostly mechanical nature, it's ok for me to do it all at once. The changes themselves should be quick to review even if it spans this many files.

@danielmitterdorfer danielmitterdorfer self-assigned this Jun 7, 2021
@pquentin
Copy link
Member Author

pquentin commented Jun 7, 2021

Thanks, I'll fix the line-too-long lint issues. (I've ran pylint, but am not familiar with the output yet, and don't understand when it fails or does not fail.)

black is on our radar but I fear that we won't have much capacity at the moment to work on it.

Is it just about the work to set it up, or are there other concerns?

@pquentin pquentin marked this pull request as draft June 8, 2021 10:08
@b-deam b-deam modified the milestones: 2.2.1, 2.3.0 Jun 23, 2021
@pquentin
Copy link
Member Author

Closing as I haven't been able to give this pull request the attention it needs, sorry! And it's going to be restarted from scratch anyway.

@pquentin pquentin closed this Jul 12, 2021
@pquentin pquentin deleted the pytest-asserts branch July 25, 2022 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Linter changes, reformatting, removal of unused code etc. :misc Changes that don't affect users directly: linter fixes, test improvements, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants