-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix source reindenting by using textwrap.dedent directly.
#4069
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
Conversation
src/_pytest/_code/source.py
Outdated
| return Source(strsrc, **kwargs) | ||
|
|
||
|
|
||
| def deindent(lines, offset=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove offset: I've added assert offset is None to the beginning of Source.deindent and the entire suite executed without triggering the assert. Can you check please if that's the case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is indeed the case, are we ok breaking this api?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dedent is in _pytest._code.source, so I don't believe it will break any code. @RonnyPfannschmidt's concern was with the Source object, which is exposed by testdir, but here it is not the case.
@RonnyPfannschmidt please correct me if I'm wrong. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah ok, I was also going to remove offset=None in Source.deindent as well, that's slightly closer to a break but still nobody should be calling it 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i all for removing that api
Codecov Report
@@ Coverage Diff @@
## master #4069 +/- ##
==========================================
+ Coverage 94.49% 94.53% +0.04%
==========================================
Files 109 109
Lines 23837 23802 -35
Branches 2361 2348 -13
==========================================
- Hits 22524 22502 -22
+ Misses 1002 996 -6
+ Partials 311 304 -7
Continue to review full report at Codecov.
|
| def __init__(self, *parts, **kwargs): | ||
| self.lines = lines = [] | ||
| de = kwargs.get("deindent", True) | ||
| rstrip = kwargs.get("rstrip", True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i believe this is a breaking api change - what where our guarantees on those internals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't an api break, the parameter is ignored (nothing is validated about **kwargs)
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
based on initial review of the necessary test changes its broken worse now
testing/code/test_source.py
Outdated
|
|
||
| lines = deindent(inspect.getsource(f).splitlines()) | ||
| assert lines == ["def f():", ' c = """while True:', " pass", '"""'] | ||
| assert lines == [" def f():", ' c = """while True:', " pass", '"""'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks pretty much broken now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same ad the test above, but in list form, I'm pretty sure this is more correct than before (before it introduced an indentation error)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i see what you mean, please make it a indented block with disabled black formatting and a comment that the indent of the last """ is the relative ancor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel I could also delete this test since the above one covers the same behavior and is more expressive 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, true actually
my missunderstanding was resolved
nicoddemus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! 👍 😄
Resolves #4066