Skip to content

Update special dataclass handling #153

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

Merged
merged 3 commits into from
Oct 7, 2020
Merged

Conversation

lihu-zhong
Copy link
Contributor

The existing unit tests appear to catch this regression when running with a python version that exhibits this change, so I didn't add an additional protection.

Please let me know I'm missing anything in this patch. Thanks for a great tool!

@lihu-zhong
Copy link
Contributor Author

The CI failures don't appear to be a regression of this patch. I get these same warnings when running tox against master:

# warning: 

WARNING: Cannot treat a function defined as a local function: "dummy_module.Class.locally_defined_callable_field"  (use @functools.wraps)

WARNING: Cannot resolve forward reference in type annotations of "dummy_module.function_with_unresolvable_annotation": name 'a' is not defined

@adamtheturtle
Copy link
Contributor

The related issue is https://github.com/agronholm/sphinx-autodoc-typehints/issues/123.

Let's also be aware that @agronholm is looking for a new maintainer (https://github.com/agronholm/sphinx-autodoc-typehints/issues/146).

I would very much like to see this issue fixed.
I'm going to have a go at a review perhaps as an attempt to help out but I do not know this codebase well.

@adamtheturtle
Copy link
Contributor

The existing unit tests appear to catch this regression when running with a python version that exhibits this change, so I didn't add an additional protection.

Is it possible to get CI to exhibit those errors? And then have this change fix tests which would otherwise fail in CI?

https://github.com/agronholm/sphinx-autodoc-typehints/pull/155 includes a fix for the CI issues you were hitting.

As a reader, I would expect _is_dataclass to behave basically the same as dataclasses.is_dataclass(obj). However, it does not and I'm not clear why. Perhaps a comment or renaming would help to answer "What is this function meant to do that is different from dataclasses.is_dataclass(obj)?".

@lihu-zhong
Copy link
Contributor Author

lihu-zhong commented Sep 10, 2020

yes. if you run CI on master with a version of python that has these changes (I'm using 3.8.5), it will show WARNING: Cannot treat a function defined as a local function on the dataclass test. Under the same conditions, adding this patch removes that failure.

The difference is the input type. obj in this context is not an instantiated dataclass object. The type is not known until runtime, hence the what variable. I can change the function name to something like _is_generated_dataclass_constructor() if you'd like something more specific?

Thanks for the review!

@adamtheturtle
Copy link
Contributor

My preference (and again, I am not a maintainer or gatekeeper on this project) is to modify test_sphinx_output as so:

  • Right now there is:
    warnings = warning.getvalue().strip()
    assert 'Cannot resolve forward reference in type annotations of ' in warnings, warnings
  • Instead, there could be something like:
warnings = warning.getvalue().strip().splitlines()
# ...  now assert something about each expected warning

This should fail (not just warn!) on CI and give a heads up ASAP when things break in future.

I can change the function name to something like _is_generated_dataclass_constructor() if you'd like something more specific?

A comment detailing exactly what you just said would be great (for me personally as a reader).

@agronholm
Copy link
Collaborator

@lbenezriravin would you mind rebasing against current master? The CI failures should be gone.

@lihu-zhong
Copy link
Contributor Author

I elaborated in the comment to clarify as per @adamtheturtle 's request. While I agree that the proposed update to the tests is probably better, IMO it's out of scope for this specific patch to refactor the unit tests.

@adamtheturtle
Copy link
Contributor

IMO it's out of scope for this specific patch to refactor the unit tests.

The reason I suggested it is I think that if there is a reported bug and there is no failing test for that bug, then an appropriate scope includes adding (or modifying) a test which would demonstrate that bug, and then fixing that test.

@adamtheturtle
Copy link
Contributor

That said - I would be more than happy to see this merged so I can remove my workaround hacks!

@lihu-zhong
Copy link
Contributor Author

Sorry for the delay. I see that the unit tests no longer fail when I remove this patch, probably due to the CI fix. You're right, adding a protection is absolutely in scope in that case. I'm a little busy for the next few days, but I'll try to get an update in soon.

winksaville referenced this pull request in winksaville/py-example-pkg-winksaville Oct 3, 2020

Verified

This commit was signed with the committer’s verified signature. The key has expired.
winksaville Wink Saville
Of note is that it was necessary to add a logger filter to suppress
a warning from incorrect code in sphinx_autodoc_typehints.

Workaround FROM:
  https://github.com/agronholm/sphinx-autodoc-typehints/issues/123#issuecomment-698314873

Probable Real Fix:
  https://github.com/agronholm/sphinx-autodoc-typehints/pull/153
@adamtheturtle
Copy link
Contributor

@lbenezriravin If you'd like to share this branch with me I'm happy to add some tests 😄

@lihu-zhong
Copy link
Contributor Author

Sorry for the delay. I added a protection for this bug and verified that my patch fixes it.

@agronholm
Copy link
Collaborator

One or more tests use f-strings which are not compatible with Python 3.5. As 3.5 is now EOL, I will drop it soon, but until I do that please use 3.5 compatible syntax.

@lihu-zhong
Copy link
Contributor Author

ah, my bad. I don't have 3.5 installed, so those tests were being skipped when I tested locally.

lihu added 2 commits October 7, 2020 09:40

Unverified

No user is associated with the committer email.
The existing test did not have any fields and missed an edge case in a
bugfix python version update.

Unverified

No user is associated with the committer email.
The dataclass generated qualnames were updated in a patch version of
python, likely the update that either caused or fixed https://bugs.python.org/issue34776

Fixes tox-dev#123
@lihu-zhong
Copy link
Contributor Author

lihu-zhong commented Oct 7, 2020

I'm not really sure how to resolve this -- python 3.5 doesn't support type hinting variables (pep526), which I need in order to catch this bug on later versions of python. Since the tests are a monolith, I'm not seeing a super clean way to skip testing dataclasses on python3.5 (since it doesn't matter there anyways).

@agronholm
Copy link
Collaborator

Add this to conftest.py:

import re
import sys

version_re = re.compile(r'_py(\d)(\d)\.py$')


def pytest_ignore_collect(path, config):
    match = version_re.search(path.basename)
    if match:
        version = tuple(int(x) for x in match.groups())
        if sys.version_info < version:
            return True

Then name the test module with a _py36.py suffix.

@lihu-zhong
Copy link
Contributor Author

Since there's only one test module, wouldn't that skip all tests for python 3.5? How is that different from just removing py35 from tox?

@agronholm
Copy link
Collaborator

The point is that you put all the tests requiring 3.6+ syntax in a new test module with the _py36 suffix.

@lihu-zhong
Copy link
Contributor Author

lihu-zhong commented Oct 7, 2020

Alright, I think I broke out the dataclass test as cleanly as possible. Thank you for your help!

edit: removed a rogue .swp file

Unverified

No user is associated with the committer email.
py35 doesn't have support for the syntax needed to test this feature
@agronholm agronholm merged commit 1de63f8 into tox-dev:master Oct 7, 2020
@agronholm
Copy link
Collaborator

Thanks!

@adamtheturtle
Copy link
Contributor

Thank you @agronholm and @lbenezriravin for your hard work on this.

@agronholm - Is it possible to have a release with this change soon? Is there anything that I can do to help?

@agronholm
Copy link
Collaborator

I'll try to get a release out within the next couple days.

@adamtheturtle
Copy link
Contributor

Thank you @agronholm 🙏

@agronholm
Copy link
Collaborator

v1.11.1 released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants