-
Notifications
You must be signed in to change notification settings - Fork 12
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
transform assert_almost_equal (a,b) into assert a == approx(b) #6
Conversation
Hi @massich thanks for contributing a PR. Github is showing me that you created a new class but this class is not used anywhere in code base, is that what you intended? I expected to see each file listed on https://github.com/pytest-dev/nose2pytest/search?utf8=%E2%9C%93&q=assert_almost_equal&type= to need fixing to support the switch to approx. And since the change is on a branch, there is no risk. |
4aef66a
to
f9ebe80
Compare
I had create this class you are talking about when we were looking to have both implementations (before and after pytest-3.0) but in the reference issue we decided to not add the option for legacy transformation unless requested. Once said that, I could make use of some help to make this PR to work. I changed the test for almost equal and changed its definition however when parsing the node I get an error. I've been trying to understand Any hints are wellcome |
Hi Joan,
The path is a sequence of indices through the node tree. To figure out the
path, You basically set a breakpoint in the transform method, and you use
the debugger to figure out the nodes you want in the destination
expression, here your destination is "a == approx(b, abs=delta)" but the
indices must target a, b and delta so these can be copied from the original
expression into the "slots" for the new expression. I use PyCharm -- at the
breakpoint, you can expand the node of the target expression to see its
children, pick the child of interest which is itself a node, and expand it
etc until you get to the nodes of interest). See if that helps. If not,
I'll take a closer look at the old code and can provide more precise
instructions. Thanks,
Oliver
…On Mon, 4 Sep 2017 at 04:45 Joan Massich ***@***.***> wrote:
I had create this class you are talking about when we were looking to have
both implementations (before and after pytest-3.0) but in the reference
issue we decided to not add the option for legacy transformation unless
requested.
Once said that, I could make use of some help to make this PR to work. I
changed the test for almost equal and changed its definition however when
parsing the node I get an error. I've been trying to understand
_get_node() in order to provide a correct path for the new translation
but I don't really get it. When I inspect the node manually the delta
value is at (0,1,1,4) instead of 2 but this does not make sense either.
Any hints are wellcome
|
That's what I did. And delta is at (0,1,1,4) instead of 2 but changing the
path to that still makes out of bounds. Which I don't get.
…On Mon, Sep 4, 2017, 22:03 schollii ***@***.***> wrote:
Hi Joan,
The path is a sequence of indices through the node tree. To figure out the
path, You basically set a breakpoint in the transform method, and you use
the debugger to figure out the nodes you want in the destination
expression, here your destination is "a == approx(b, abs=delta)" but the
indices must target a, b and delta so these can be copied from the original
expression into the "slots" for the new expression. I use PyCharm -- at the
breakpoint, you can expand the node of the target expression to see its
children, pick the child of interest which is itself a node, and expand it
etc until you get to the nodes of interest). See if that helps. If not,
I'll take a closer look at the old code and can provide more precise
instructions. Thanks,
Oliver
On Mon, 4 Sep 2017 at 04:45 Joan Massich ***@***.***> wrote:
> I had create this class you are talking about when we were looking to
have
> both implementations (before and after pytest-3.0) but in the reference
> issue we decided to not add the option for legacy transformation unless
> requested.
>
> Once said that, I could make use of some help to make this PR to work. I
> changed the test for almost equal and changed its definition however when
> parsing the node I get an error. I've been trying to understand
> _get_node() in order to provide a correct path for the new translation
> but I don't really get it. When I inspect the node manually the delta
> value is at (0,1,1,4) instead of 2 but this does not make sense either.
>
> Any hints are wellcome
>
> —
> You are receiving this because you commented.
>
>
> Reply to this email directly, view it on GitHub
> <#6 (comment)
>,
> or mute the thread
> <
https://github.com/notifications/unsubscribe-auth/ACenmH8SdWMCrTBqYekBvVfDRtUYhkxlks5se7jBgaJpZM4PBotg
>
> .
>
--
Oliver
My StackOverflow contributions
My CodeProject articles
My Github projects
My SourceForget.net projects
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGt-48TIM_PlYOZfXsZLTjVOCqbsMNrGks5sfFd3gaJpZM4PBotg>
.
|
If so then I can have a look at it.
…On Mon, 4 Sep 2017 at 21:54 oliver ***@***.***> wrote:
Did you commit your latest code on your branch?
On Mon, 4 Sep 2017 at 16:55 Joan Massich ***@***.***> wrote:
> That's what I did. And delta is at (0,1,1,4) instead of 2 but changing the
> path to that still makes out of bounds. Which I don't get.
>
> On Mon, Sep 4, 2017, 22:03 schollii ***@***.***> wrote:
>
> > Hi Joan,
> > The path is a sequence of indices through the node tree. To figure out
> the
> > path, You basically set a breakpoint in the transform method, and you
> use
> > the debugger to figure out the nodes you want in the destination
> > expression, here your destination is "a == approx(b, abs=delta)" but the
> > indices must target a, b and delta so these can be copied from the
> original
> > expression into the "slots" for the new expression. I use PyCharm -- at
> the
> > breakpoint, you can expand the node of the target expression to see its
> > children, pick the child of interest which is itself a node, and expand
> it
> > etc until you get to the nodes of interest). See if that helps. If not,
> > I'll take a closer look at the old code and can provide more precise
> > instructions. Thanks,
> > Oliver
> >
> > On Mon, 4 Sep 2017 at 04:45 Joan Massich ***@***.***>
> wrote:
> >
> > > I had create this class you are talking about when we were looking to
> > have
> > > both implementations (before and after pytest-3.0) but in the
> reference
> > > issue we decided to not add the option for legacy transformation
> unless
> > > requested.
> > >
> > > Once said that, I could make use of some help to make this PR to
> work. I
> > > changed the test for almost equal and changed its definition however
> when
> > > parsing the node I get an error. I've been trying to understand
> > > _get_node() in order to provide a correct path for the new translation
> > > but I don't really get it. When I inspect the node manually the delta
> > > value is at (0,1,1,4) instead of 2 but this does not make sense
> either.
> > >
> > > Any hints are wellcome
|
Did you commit your latest code on your branch?
…On Mon, 4 Sep 2017 at 16:55 Joan Massich ***@***.***> wrote:
That's what I did. And delta is at (0,1,1,4) instead of 2 but changing the
path to that still makes out of bounds. Which I don't get.
On Mon, Sep 4, 2017, 22:03 schollii ***@***.***> wrote:
> Hi Joan,
> The path is a sequence of indices through the node tree. To figure out
the
> path, You basically set a breakpoint in the transform method, and you use
> the debugger to figure out the nodes you want in the destination
> expression, here your destination is "a == approx(b, abs=delta)" but the
> indices must target a, b and delta so these can be copied from the
original
> expression into the "slots" for the new expression. I use PyCharm -- at
the
> breakpoint, you can expand the node of the target expression to see its
> children, pick the child of interest which is itself a node, and expand
it
> etc until you get to the nodes of interest). See if that helps. If not,
> I'll take a closer look at the old code and can provide more precise
> instructions. Thanks,
> Oliver
>
> On Mon, 4 Sep 2017 at 04:45 Joan Massich ***@***.***>
wrote:
>
> > I had create this class you are talking about when we were looking to
> have
> > both implementations (before and after pytest-3.0) but in the reference
> > issue we decided to not add the option for legacy transformation unless
> > requested.
> >
> > Once said that, I could make use of some help to make this PR to work.
I
> > changed the test for almost equal and changed its definition however
when
> > parsing the node I get an error. I've been trying to understand
> > _get_node() in order to provide a correct path for the new translation
> > but I don't really get it. When I inspect the node manually the delta
> > value is at (0,1,1,4) instead of 2 but this does not make sense either.
> >
> > Any hints are wellcome
|
The branch is clean you can give it a look
…On Tue, Sep 5, 2017, 03:55 schollii ***@***.***> wrote:
Did you commit your latest code on your branch?
On Mon, 4 Sep 2017 at 16:55 Joan Massich ***@***.***> wrote:
> That's what I did. And delta is at (0,1,1,4) instead of 2 but changing
the
> path to that still makes out of bounds. Which I don't get.
>
> On Mon, Sep 4, 2017, 22:03 schollii ***@***.***> wrote:
>
> > Hi Joan,
> > The path is a sequence of indices through the node tree. To figure out
> the
> > path, You basically set a breakpoint in the transform method, and you
use
> > the debugger to figure out the nodes you want in the destination
> > expression, here your destination is "a == approx(b, abs=delta)" but
the
> > indices must target a, b and delta so these can be copied from the
> original
> > expression into the "slots" for the new expression. I use PyCharm -- at
> the
> > breakpoint, you can expand the node of the target expression to see its
> > children, pick the child of interest which is itself a node, and expand
> it
> > etc until you get to the nodes of interest). See if that helps. If not,
> > I'll take a closer look at the old code and can provide more precise
> > instructions. Thanks,
> > Oliver
> >
> > On Mon, 4 Sep 2017 at 04:45 Joan Massich ***@***.***>
> wrote:
> >
> > > I had create this class you are talking about when we were looking to
> > have
> > > both implementations (before and after pytest-3.0) but in the
reference
> > > issue we decided to not add the option for legacy transformation
unless
> > > requested.
> > >
> > > Once said that, I could make use of some help to make this PR to
work.
> I
> > > changed the test for almost equal and changed its definition however
> when
> > > parsing the node I get an error. I've been trying to understand
> > > _get_node() in order to provide a correct path for the new
translation
> > > but I don't really get it. When I inspect the node manually the delta
> > > value is at (0,1,1,4) instead of 2 but this does not make sense
either.
> > >
> > > Any hints are wellcome
> > >
> > > —
> > > You are receiving this because you commented.
> > >
> > >
> > > Reply to this email directly, view it on GitHub
> > > <
> #6 (comment)
> > >,
> > > or mute the thread
> > > <
> >
>
https://github.com/notifications/unsubscribe-auth/ACenmH8SdWMCrTBqYekBvVfDRtUYhkxlks5se7jBgaJpZM4PBotg
> > >
> > > .
> > >
> > --
> > Oliver
> > My StackOverflow contributions
> > My CodeProject articles
> > My Github projects
> > My SourceForget.net projects
> >
> > —
> > You are receiving this because you were mentioned.
> > Reply to this email directly, view it on GitHub
> > <
#6 (comment)
> >,
> > or mute the thread
> > <
>
https://github.com/notifications/unsubscribe-auth/AGt-48TIM_PlYOZfXsZLTjVOCqbsMNrGks5sfFd3gaJpZM4PBotg
> >
> > .
> >
>
> —
> You are receiving this because you commented.
>
>
> Reply to this email directly, view it on GitHub
> <#6 (comment)
>,
> or mute the thread
> <
https://github.com/notifications/unsubscribe-auth/ACenmEQ1wGcyI11IshxAGRkku2h3KzTEks5sfGPMgaJpZM4PBotg
>
> .
>
--
Oliver
My StackOverflow contributions
My CodeProject articles
My Github projects
My SourceForget.net projects
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGt-4z43ZPNOKlG7YcqlsvKfRRVe7NGWks5sfKoMgaJpZM4PBotg>
.
|
@schollii any hints? |
Sorry Massich just been very busy with work. I'll have a look after work today or during the weekend, i'd really like to close this because it'll probably indicate missing documentation or hopefully a simple tweak will be sufficient to cover this case. |
I didn't manage to understand properly the structure of I was planning to use |
…e2pytest into massich-almost_equal_to_approx
@massich the paths were actually the easy part to fix, I will add a description of steps in the readme doc on how to do that. The tougher part was that once I had fixed the paths, the spacing was wrong: I'm still new to collab on github (I haven't had to deal with PR's until now). If I had not had to make adjustments, I could just merge your PR and close it. I had to make changes, so if I commit those and push them to my master branch, I end up having to reject your PR and close it, which I rather not do because then it will seem like your contribution was rejected. Any ideas? |
Actually its really easy, since you have full rights you can push directly to the PR branch. Your commits would go on the top of mine. In this manner if there's something else to be done like write some more tests I can take over. |
No kidding, that's pretty cool. So what I did so far is this:
I did not yet commit my edits yet, so it sounds like I would do the following:
From there we would both work like this on your PR branch, then eventually I would accept and merge your PR. When I merge it via the github web UI, does git automatically use the HEAD of the PR branch? BTW do you know of a good explanation of this workflow somewhere, the github book only seems to cover the case of "contributor-creates-PR-then-author-merges-it". |
4 and 5 seems like the right thing. If there's any trouble I give you rights on my fork, but you should not need them since you own the main project. (see here) Then when you click the green button everything would be squashed and merged :) and we both would own the changes. And if we ever need to get back to it, github keeps the history of the squash :) so that we can restore the PR even after closed. |
I tried
If I add a colon before the branch name, the output is the following:
Why is it saying your branch has been deleted? Note the -n as I just wanted to try the commands. |
I did a simple test with a collegue, and I had no problem. Either way it is strange that you cannot push on the PR, maybe its because the project belongs to a pytest-dev and you might not have rights or something. We can try to figure it out. Can you create a dummy project on your account, I'll PR and you try to commit to the PR. edit: Actually I've create a PR to one of your projects here |
Figured it out based on https://stackoverflow.com/questions/8479559/github-pushing-to-pull-requests:
Looks like Github is tracking that branch as this PR page now indicates that all checks pass, pretty cool. The only thing that is not clear is whether this would have worked without you having given me permission. It would be worth trying again: if you remove permission you added, I will push another mod. <rant>I've been using RCS then CVS then Subversion for years and I must say, there are some very cool things about git, but a lot of things go way overboard in complexity. Simple things should be simple to do, complicated things should be possible. Some simple things are simple: branching, merging. But lots of other simple things are not, like what we were trying to do with this PR. And various related discussions on stackoverflow mention rebasing and forcing and leases, makes my head explode. I'm willing to believe that as I use git more, I'll be fine with those things, but it seems like it could be costing me way less time.</rant> |
git is a great piece of software. I've removed you from my fork. |
It worked still, good to know. How about I extend the docs to discuss node paths, then you have a look (add some review comments -- I'd love to try github's collaborative review feature -- and, if you wish, make whatever fixes), then we should be good to merge and close the PR? |
If you could take a quick look at the updated docs (README.rst file), that would be much appreciated. I'm hoping we can close this PR soon then I can create another release. |
good. I'll try to give it a go either today or tomorrow. |
Sorry I didn't got to it earlier. I would change the way this is explained. But I'm not sure it is worth the effort. What I mean is that I guess that with what you wrote I would had enough to figure out how to modify what I was needing. So I would get it in as it is. And then change the documentation if someone issues an issue or asks questions. So far, the only one who had asked something has been me. And I wish I had the little section you wrote. Thanks a lot. |
Have you tried running it on your code base to see that it converts all the
almostequals properly? I've uncovered bugs in the past when I did that,
there's nothing like a real code base!
…On Mon, Nov 27, 2017, 09:31 Joan Massich, ***@***.***> wrote:
Sorry I didn't got to it earlier. I would change the way this is
explained. But I'm not sure it is worth the effort. What I mean is that I
guess that with what you wrote I would had enough to figure out how to
modify what I was needing.
So I would get it in as it is. And then change the documentation if
someone issues an issue or asks questions. So far, the only one who had
asked something has been me. And I wish I had the little section you wrote.
|
it is actually not working at all :) it doesn't get triggered. And more over what I was trying to translate was not even I'm using this random code to try |
I checked the code, but can't run the script on it from here so I'm just guessing:
It may be worth merging this pull request and then creating a separate one for the above if you dig into it. |
5bbef4c
to
116f2bc
Compare
nose2pytest/script.py
Outdated
wrapped_delta_val = wrap_parens_for_comparison(delta_val) | ||
dest3.replace(wrapped_delta_val) | ||
|
||
elif delta.children[0] == PyLeaf(token.NAME, 'places'): |
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've never modified the PATTERN_ALMOST_ARG
.
nose2pytest/nose2pytest/script.py
Lines 74 to 78 in 304d875
PATTERN_ALMOST_ARGS = """ | |
power< '{}' trailer< '(' | |
( arglist< aaa=any ',' bbb=any ',' delta=any [','] > | |
| arglist< aaa=any ',' bbb=any ',' delta=any ',' msg=any > ) | |
')' > > |
tests/test_script.py
Outdated
check_transformation('assert_almost_equal(123.456, 124, decimal=7)', | ||
'assert 123.456 == pytest.approx(124, abs=1e-7)') | ||
# check_passes(refac, | ||
# 'assert_almost_equal(123.456, 123.450, decimal=1)', |
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.
To use this one, we need to add numpy
's assert_almost_equal
. I need to check how to do that. Meanwhile the transformation is already ok.
What I'm not sure is the same as in the case of places, which if we compare 123.456
to 123.400
up to the first decimial it fails. Not sure why, maybe I'm not understanding the behavior of decimal or place properly
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.
One issue that I had not thought of is that the script is meant to convert nose assertions to pytest assertions. Including assertions from other tools (like numpy) opens a can of worms, and we have to think this through. What is the relationship there, does e.g. numpy explicitely support nose assertions? (in this case the policy could be that any lib that supports nose assertions can be supported).
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.
the signature of almost equal is almost the same. see here. It only changes places
parameter by decimal
, and adds an extra parameter verbose.
Another thing that I was thinking was that it might be configured which assertions to translate. In case you only want to target some specific transformation. For instance to split the changes across commits or simply because you prefer to keep some asserts that had been rewritten by some other package.
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.
maybe we can merge to master removing this part of numpy
, publish the release and keep working on it. We would support nosetest
and I can add this part on my fork.
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.
But what is the motivation for transforming a numpy assertion to a raw assertion? The point of doing that when using pytest is that pytest traps raw assertions and prints useful info, so after the conversion one can eliminate nose as a dependency.
But numpy assertions are designed to work with numpy data structures (arrays etc) so typically there would not be a good way of expressing them as equivalent raw assertions. For example the numpy.test.assert_almost_equal would almost certainly work with numpy arrays of any dimension, as well as scalars. So it would likely be incorrect to transform all occurrences of numpy's assert_almost_equal.
I think it makes sense to transform the nose.tools.assert_almost_equal to use pytest's approx, but the numpy one, I just don't see how it belongs in nose2pytest. Are you actually able to use your latest version on your code base to your satisfaction?
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.
Actually you are right. Changing numpy's might not bring usefull information. So how do we ensure that it acutally translate only those comming from nosetest?
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.
It is not possible at the AST stage to know which module the assert_almost_equal belongs to. There fore, there is no way of preventing inadvertent transformations when the same symbol name is defined in more than one module (such as nose.tools and numpy.tests). There is also no reliable way of knowing whether the arg is a scalar or numpy array. Based on https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.testing.assert_almost_equal.html, it would therefore be imprudent to transform an assert_almost_equal that has a "places" or "decimals" argument.
In fact, this whole discussion makes me realize that the code in script.py should be extended to return the assertion node unchanged if there are arg names that are not recognized as being those of a nose assertion. This would apply to all nose assertions, not just assert_almost_equal. I'm guessing that such check would be done in the transform(), based on an "allowed arg names" field defined for each assertion, by checking the keys in "results" dict, although part of the check implementation may have to be in the derived class.
So unless I'm misunderstanding your argument, the only part of your latest commit that can be kept for this PR is the version command line ARG. If you'd like to try the arg names check, please do it as a separate PR.
116f2bc
to
cedca1d
Compare
I think we can integrate this one. |
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.
Looking forward to getting this merged!
@cclauss hey do I need to merge this or will one of you guys do it when you are ready for next pytest release? |
I am not a maintainer of this repo so the checkmark on my review is black, not green. This PR needs the approval of a maintainer. |
@cclauss I'm the author of this repo so I might be able to approve it, but I am not familiar with the pytest approval process, so let me know if I can speed this up |
I can chime in: the In general we (the pytest maintainers) suggest you have at least 1 approval and passing CI before merging a PR. |
OK. Recommended steps based on the last two comments on this thread...
|
@schollii would you like to do the honors? Otherwise I will go ahead and approve/merge/release in the next few days. |
Sure I'll have a look at merging this |
I finally got around to checking this. It no longer passes because pytest no longer has This was a bit tricky to fix but not too bad, just needed a bit of refamil. I'm about to push a fix onto this PR. |
@massich @nicoddemus @cclauss Please see my previous comment to understand the following :) Fixes done and all tests now pass. I couldn't resolve the conflicts via the fork so I did it by pulling massich changes into my repo. Good news is that @massich commits show up as his, and this PR shows up as merged. I'll address any failures with tox, if that is still in use (it's been such a long time, and I saw a commit message about github-actions somewhere). |
fixes #5