Skip to content

Indentation error while parsing version-conditional imports #178

@achimnol

Description

@achimnol

With autodoc (.. automodule:: ...),

if sys.version_info < (3, 8, 0):
    from typing_extensions import Literal  # this line
else:
    from typing import Literal

causes "expected indented block" error.

To Reproduce
Steps to reproduce the behavior:

<Paste your command-line here which cause the problem>

$ git clone https://github.com/achimnol/aiotools
$ cd aiotools
$ pip install -r requirements/dev.txt
$ cd docs
$ make html

Expected behavior
It should build the docs.

Screenshots
image

Environment info

  • OS: Linux (Ubuntu 20.04)
  • Python version: 3.9.5
  • Sphinx version: 4.0.1, 4.1.0dev installed from GitHub
  • Sphinx extensions: sphinx.ext.autodoc, sphinx_autodoc_typehints

Activity

cblegare

cblegare commented on Sep 9, 2021

@cblegare
Contributor

Got a similar error when I have class definitions nested in functions

def some_function():
    class SomeClass:
        pass
Extension error (sphinx_autodoc_typehints):
Handler <function process_docstring at 0x7f943cb6c9d0> for event 'autodoc-process-docstring' threw an exception (exception: expected an indented block (<unknown>, line 4))
cblegare

cblegare commented on Sep 13, 2021

@cblegare
Contributor

Found the root cause in my case. To reproduce, document an async function wih a nested function definition.

Example

async def async_function():
    class InnerClass:
        def __init__(self): pass

the normalize_source_lines function does not detect async def as a function definition., thus provide invalid python as output:

        
        async def async_function():
            class InnerClass:
        def __init__(self): pass
        

I'll try to submit a quick PR about this..

@achimnol that might fix it fr you too

cblegare

cblegare commented on Sep 13, 2021

@cblegare
Contributor
achimnol

achimnol commented on Dec 19, 2021

@achimnol
Author

This is still happening with Sphinx 4.3 and sphinx-autodoc-typehints 1.12.0, on Python 3.10.
@cblegare's PR is about fixing the issue only for async def lines, but my issue is unrelated with them but just a version-conditional import. Changing my import statement to use try: ... except ImportError: ... gives me the same error.

gaborbernat

gaborbernat commented on Jan 8, 2022

@gaborbernat
Member

A PR for this would we welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @achimnol@gaborbernat@cblegare

        Issue actions

          Indentation error while parsing version-conditional imports · Issue #178 · tox-dev/sphinx-autodoc-typehints