Skip to content

2.2.0 Throws Exception for class with custom metaclass #462

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

Closed
b-phi opened this issue Jun 19, 2024 · 14 comments
Closed

2.2.0 Throws Exception for class with custom metaclass #462

b-phi opened this issue Jun 19, 2024 · 14 comments

Comments

@b-phi
Copy link

b-phi commented Jun 19, 2024

Handler <function process_docstring at 0x7f12ae4c8b80> for event 'autodoc-process-docstring' threw an exception (exception: unhashable type: '_SchemaMeta')

I've including the relevant skeleton of the classes below.

class _SchemaMeta(type):
    def __new__(mcs, *args: Any, **kwargs: Any) -> '_SchemaMeta':
        # some logic
        return schema_cls

    def __eq__(cls, other: Any) -> bool:
        ...

class Schema(metaclass=_SchemaMeta):
    ...

Elsewhere in the code, we have this which causes the error

class Foo:
    def do_something(self, schema: Schema) -> None:
        """
        Args:
            schema: Some schema.
        """
        ...

Edit: I've pinned to 2.1.1 for now, which doesn't throw an error in this case.

@hoodmane
Copy link
Collaborator

@tikuma-lsuhsc seems likely a regression caused by #459.

@tikuma-lsuhsc
Copy link
Contributor

Yep, I did not account for a string annotation as I wasn't even aware such is even an option. I'll look into it tonight.

@tikuma-lsuhsc
Copy link
Contributor

tikuma-lsuhsc commented Jun 20, 2024

@b-phi - I tested all three class def's of yours but none produced the error that you reported.

Built with sphinxcontrib-restbuilder (for convenience as I'm using it for my current project), your code snippet produced the following without any error or warning.

**class mod._SchemaMeta(*args: Any, **kwargs: Any)**

**class mod.Schema**

**class mod.Foo**

   **do_something(mod: Schema) -> None**

      :Parameters:
         **schema** (`Schema <#mod.Schema>`_) – Some
         schema.

      :Return type:
         ``None``

with the autodoc command in the input rst file:

.. autoclass:: mod._SchemaMeta
   :members:

.. autoclass::  mod.Schema
   :members:

.. autoclass::  mod.Foo
   :members:

I've also written pytest cases within the repo without any success. Could you disclose more info? (relevant conf.py entry & the rst lines come to my mind). Thanks.

@bartfeenstra
Copy link

I am getting the following, very similar error:

Extension error (sphinx_autodoc_typehints):
Handler <function process_docstring at 0x7e7cf5e92520> for event 'autodoc-process-docstring' threw an exception (exception: unhashable type: 'typing.ParamSpecArgs')

This is also since 2.2.0 (pinning to 2.1.1 avoids the bug). I have not been able to determine the exact code sphinx-autodoc-typehints trips over. If and when I do, I'll share an example here to help reproduce the problem.

@b-phi
Copy link
Author

b-phi commented Jun 20, 2024

As requested, some additional info. Will try to find some time to put together a reproducible example

conf.py sections

extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.autosectionlabel',
    'sphinx.ext.napoleon',
    'sphinx_autodoc_typehints',
]

# Autodoc Configuration
autodoc_default_options = {
    'members': True,
    'member-order': 'bysource',
    'special-members': '__init__',
    'undoc-members': True,
}

# Typehint Configuration
typehints_defaults = 'comma'
typehints_use_signature = True
typehints_use_signature_return = True

rst where Foo is the one in my original comment

...

.. autoclass:: path.to.class.Foo

@tikuma-lsuhsc
Copy link
Contributor

@b-phi - Thanks. Those options still won't trigger the error, unfortunately. The same goes with @bartfeenstra's conf.py settings in the linked repo.

Meanwhile, I have a fix candidate pushed to my fork:

git+https://github.com/tikuma-lsuhsc/sphinx-autodoc-typehints/tree/fix-issue-462

If possible, could you try the above branch on your projects and see if it resolves the bug?

@b-phi
Copy link
Author

b-phi commented Jun 20, 2024

Hey @tikuma-lsuhsc, unfortunately this branch does not resolve the error

@tikuma-lsuhsc
Copy link
Contributor

@bartfeenstra - hmm, that's unfortunate. Regarding your error, do you have a snippet of 2.1.1 output which contains 'typing.ParamSpecArgs' (or ParamSpecArgs)? If so, could you point to the line on your repo as well as the expected output text (html or otherwise)? Thanks

@b-phi
Copy link
Author

b-phi commented Jun 20, 2024

Hey @tikuma-lsuhsc I've created a minimal example. Overriding __eq__ on the metaclass seems to be required to reproduce for me

from typing import Any


class _SchemaMeta(type):
    def __eq__(cls, other: Any) -> bool:
        return True


class Schema(metaclass=_SchemaMeta):
    pass


def f(s: Schema) -> Schema:
    """
    Do something.

    Args:
        s: Some schema.
    """
    return s

@tikuma-lsuhsc
Copy link
Contributor

@b-phi - Confirmed! Thanks for putting the example together.

tikuma-lsuhsc added a commit to tikuma-lsuhsc/sphinx-autodoc-typehints that referenced this issue Jun 20, 2024
@tikuma-lsuhsc
Copy link
Contributor

tikuma-lsuhsc commented Jun 20, 2024

@b-phi & @bartfeenstra - I think I have a solution to the issue. If you could please help me test again with my fork (don't forget to update if tried once above):

git+https://github.com/tikuma-lsuhsc/sphinx-autodoc-typehints/tree/fix-issue-462

Thanks!

@b-phi
Copy link
Author

b-phi commented Jun 20, 2024

That seems to have fixed it, thanks!

@jmccreight
Copy link

This also appears to have fix issues I had (I dont know what they were). Thanks.

tikuma-lsuhsc added a commit to tikuma-lsuhsc/sphinx-autodoc-typehints that referenced this issue Jun 21, 2024
gaborbernat pushed a commit that referenced this issue Jun 21, 2024
* test update (non-reproducible)

* fix candidate #1

* fixes issue #462

* Fix annotation check exception (#462)
Fix processing type aliases

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* reverted test_integration.py

* make _SchemaMeta hashable for ruff pre-commit test

* pre-commit ci fix try no 2

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* _get_type_hint(): fixed the type  hint of localns arg

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@hoodmane
Copy link
Collaborator

Closed by #463.

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

No branches or pull requests

5 participants