-
Notifications
You must be signed in to change notification settings - Fork 590
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
Teach the Ghostwriter about @staticmethod and @classmethod methods #3354
Teach the Ghostwriter about @staticmethod and @classmethod methods #3354
Conversation
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.
Off to a good start 🙂
6abd9c3
to
c08d3c5
Compare
@Zac-HD I have a question since a class is also callable, I think in the previous logic, if you put a class as input it will write a test as if it is a function but if you put the module as input it will not be discovered. Do you mind clarifying what is the desired behaviour for the class itself? |
I think the previous behavior is fine here; but I'm pretty happy going with whatever is easiest to implement - the Ghostwriter is really aimed at making it easy to get started, so I care a lot about handling functions and very little about classes (because just creating an instance is rarely a useful test). |
If that is the case, I would suggest not create a test for the class. If
someone input the class, the magic will try to discover if there is any
static/ class method for tests.
…On Mon, 23 May 2022 at 03:22, Zac Hatfield-Dodds ***@***.***> wrote:
I think the previous behavior is fine here; but I'm pretty happy going
with whatever is easiest to implement - the Ghostwriter is really aimed at
making it easy to get started, so I care a lot about handling functions and
very little about classes (because just creating an instance is rarely a
useful test).
—
Reply to this email directly, view it on GitHub
<#3354 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AG3N26OV3YRKR53FLSOSFUDVLLTXHANCNFSM5WNU5QIQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
6b91652
to
bb1b526
Compare
|
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.
Code comments below; I also took a couple of attempts and eventually (after looking at the Sphinx source code!) managed to get the classmethod and staticmethod cross-references to link properly 😅
pytest.param( | ||
("xml_etree_ElementTree_py38", ghostwriter.magic(xml.etree.ElementTree)), | ||
marks=[ | ||
pytest.mark.skipif( | ||
sys.version_info[:2] != (3, 8), | ||
reason="output generated with Python 3.8", | ||
) | ||
], | ||
), | ||
pytest.param( | ||
("xml_etree_ElementTree_py310", ghostwriter.magic(xml.etree.ElementTree)), |
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'd try defining a class in this module, and use that - it works for the with_docstring
function, for example.
Looking good! I think the 3.11 failure is a genuine problem somewhere in either Pytest or maybe Hypothesis (pre-dating thia PR), so I'll aim to investigate and fix that in the next few days. And thanks again for sticking with this long process 🤩 |
Yep, the 3.11 crash was an upstream CPython-nightly issue affecting Pytest, which has since been fixed: pytest-dev/pytest#10008. |
Will closing and reopening unstick CI? Let's find out! |
Welp, looks like GitHub Actions is pretty seriously degraded at the moment... I'll restart again when it recovers 🙃 |
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.
We're very close now - just some minor comments and a request for, well, more comments on the tests 🙂
("hypothesis.strategies.builds", lambda: fuzz(hypothesis.strategies.builds)), | ||
# Discover methods in class | ||
("hypothesis.errors.StopTest", lambda: magic(StopTest)), | ||
# Imports submodule (importlib.import_module passes; __import__ fails) | ||
("hypothesis.errors.StopTest", lambda: fuzz(StopTest)), | ||
("hypothesis.strategies", lambda: magic(hypothesis.strategies)), |
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.
What are each of these new tests checking for? Is that comment about import semantics still correct, even though we've changed what we're testing?
I'd prefer to just swap magic(StopTest)
for fuzz(StopTest)
, but if we're adding more we also need to add corresponding comments.
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.
Because there's no classmethod and staticmethod in StopTest so it didn't work for fuzz before (the CLI will show "nothing to test" and fuzz will have an error) this is due to the class itself was not tested in the previous generation of this PR
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.
What are each of these new tests checking for?
I think the comment above kinda indicates something? for the magic(hypothesis.strategies)
it is a submodule and for magic(StopTest)
it discovers the methods in class (though I think it's a bad example as there's nothing in StopTest I believe)
( | ||
"my_func", | ||
"XX", | ||
"Found the 'mycode' module and 'my_func' attribute, but it doesn't have a 'XX' attribute.", | ||
), |
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.
You could compress this parametrize down to just ("my_func", " and 'my_func' attribute", "attribute")
- with a bit more string formatting. That would make it much more compact, which I think would be easier to read.
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.
Do you mean the error message? Can't quite get what you mean here 🙃
@Zac-HD Looks like GH action CI is broken.... https://github.com/HypothesisWorks/hypothesis/runs/6704398671?check_suite_focus=true#step:1:37 |
c0687b2
to
d6a999a
Compare
d6a999a
to
dfee405
Compare
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'm back from a busy weekend, and we're completely done and ready to merge! Congrats Cheuk; it turned out to be a long and twisty one but a great contribution in the end 😁:tada:
Thank you for your patient guidance @Zac-HD |
In this PR:
module_a.ClassA.function
closes #3318