Skip to content
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

Parsing error when using dataclasses in Py36 without default values #4914

Closed
theendsofinvention opened this issue Apr 29, 2018 · 11 comments
Closed

Comments

@theendsofinvention
Copy link

theendsofinvention commented Apr 29, 2018

Subject: sphinx build fails when using dataclasses in Py36 without default values

Problem

Using Python 3.6.5 on Win7 inside a Pipenv venv.

When building the documentation, Sphinx is unable to parse a .py file containing a simple dataclass (see .py file below).

    raise PycodeError('parsing %r failed: %r' % (self.srcname, exc))
sphinx.errors.PycodeError: parsing ...\\agent_loop_args.py' failed: AssertionError()

Procedure to reproduce the problem

make html

(nothing fancy going on)

Error logs / results

https://pastebin.com/qQi0NLW4

Expected results

I expected Sphinx to document the dataclass, showing the attributes' expected types.

Reproducible project / your project

import threading
import dataclasses


@dataclasses.dataclass
class AgentLoopArgs:
    """
    Defines the parameters necessary for an Agent to run.
    """
    on_start: callable
    on_stop: callable
    target: callable
    target_args: tuple
    target_kwargs: dict
    delay: int
    name: str
    stop_event: threading.Event

Environment info

  • OS: Win7
  • Python version: 3.6.5
  • Sphinx version: 1.7.4

Extensions:

#   alabaster (0.7.10) 
#   sphinx.ext.autodoc (1.7.4) 
#   sphinx.ext.intersphinx (1.7.4) 
#   sphinx.ext.todo (1.7.4)
#   sphinx.ext.coverage (1.7.4)
#   sphinx.ext.mathjax (1.7.4)
#   sphinx.ext.ifconfig (1.7.4)
#   sphinx.ext.viewcode (1.7.4)
#   sphinx.ext.githubpages (1.7.4)

This is my first time reporting an issue here, please let me know if I've broken any guideline and I'll do my best to fix it!

EDIT: I forgot to emphasize that if I'm specifying default values like so:

import threading
import dataclasses


@dataclasses.dataclass
class AgentLoopArgs:
    """
    Defines the parameters necessary for an Agent to run.
    """
    on_start: callable = None
    on_stop: callable = None
    ...

then Sphinx has no issue whatsoever building the documentation.

@tk0miya
Copy link
Member

tk0miya commented Apr 29, 2018

PEP-557 says dataclasses is a new feature of 3.7.
https://www.python.org/dev/peps/pep-0557/

But it seems you're using py3.6

Python version: 3.6.5

@theendsofinvention
Copy link
Author

@tk0miya indeed ! I'm using https://github.com/ericvsmith/dataclasses to emulate the behaviour.

The PEP states that the syntax is:

@dataclass
class C:
    a: int       # 'a' has no default value
    b: int = 0   # assign a default value for 'b'

, which is the exact same syntax I'm using.

I know this isn't official Python (yet), but since it's coming pretty soon, I figured I'd raise the issue =)

@tk0miya
Copy link
Member

tk0miya commented May 3, 2018

On my local, with python-3.6.4 and dataclasses-0.5, I can't reproduce the error.

BTW, the error was raised on parssing AnnAssign (assignment with variable annotation). So I guess this is not related with dataclasses.
Could you find the reason of this error by cutting down your code?

...
  File "...sphinx\pycode\parser.py", line 343, in visit_AnnAssign
    self.visit_Assign(node)  # type: ignore
  File "...sphinx\pycode\parser.py", line 312, in visit_Assign
    parser.parse()
  File "...sphinx\pycode\parser.py", line 229, in parse
    assert self.current
AssertionError

@tk0miya
Copy link
Member

tk0miya commented May 3, 2018

Ah, sorry. I noticed both a and b are AnnAssign statements. AnnAssign statement can omit rvalue.

@dataclass
class C:
    a: int       # 'a' has no default value
    b: int = 0   # assign a default value for 'b'

Anyway, I can't reproduce the error yet. So we have to reproduce it to fix.

@tk0miya tk0miya added this to the 1.7.5 milestone May 3, 2018
@theendsofinvention
Copy link
Author

Thanks for looking into it. I'm away for work but I'll look into it this week end and try to narrow it down!

@tk0miya
Copy link
Member

tk0miya commented May 18, 2018

We will ship next stable release soon. I'd like to fix this until then if possible. So please let me know if any updates.

@theendsofinvention
Copy link
Author

I'm sorry I didn't come back to you earlier, something popped up on another front. Thank you for the reminder !

I am at a complete loss, the bug has disappeared without any reason.

My environment is locked, and conf.py hasn't changed. I just went and checked out every commit in my history from the day I reported the issue, and on every one of them sphinx builds successfully.

I have not the slightest idea about what could have gone wrong at the time ... I will however report back if it ever happens again.

I'm sorry for wasting your time on this without an actual outcome.

@tk0miya
Copy link
Member

tk0miya commented May 20, 2018

I noticed the error has been happened silently. We can see it with -vvv option.

[autodoc] module analyzer failed: parsing '/Users/tkomiya/work/tmp/doc/zzz/foo.py' failed: AssertionError()

tk0miya added a commit to tk0miya/sphinx that referenced this issue May 20, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…thout default values
tk0miya added a commit to tk0miya/sphinx that referenced this issue May 20, 2018
@theendsofinvention
Copy link
Author

Oh, ok.

I wonder why it decided to pop for me (I'm not using -vvv). It was crashing my build entirely.

Please let me know if I can do anything to help.

Thanks a bunch for what you're doing guys, it's much, much appreciated! I'm looking forward using the upcoming stable release!

@tk0miya
Copy link
Member

tk0miya commented May 20, 2018

Please try #4996 and confirm it works fine if you have time.
I'd like to merge it to next stable release :-)

tk0miya added a commit that referenced this issue May 23, 2018
Fix #4914: autodoc: Parsing error when using dataclasses without default values
@tk0miya
Copy link
Member

tk0miya commented May 23, 2018

Fixed by #4996.
Thank you for reporting.

@tk0miya tk0miya closed this as completed May 23, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants