sphinx-autoapi icon indicating copy to clipboard operation
sphinx-autoapi copied to clipboard

`Cannot resolve import` on re-imported class from module.

Open ashb opened this issue 3 years ago • 3 comments

In airflow we like to run with -W flag (to turn warnings in to errors) and we are "stuck" on 1.0.0 for various reasons. (One day soon we'll be able to upgrade!)

This was actually an error/warning that first appeared in 1.1.0, but is still there in 1.8.0

What the export is doesn't matter, this was just what triggered it first in Airflow.

Setup:

mkdir -p airflow/
touch airflow/__init__.py
cat > airflow/typing_compat.py <<EOF
try:
    from typing import Protocol, TypedDict, runtime_checkable  # type: ignore # noqa
except ImportError:
    from typing_extensions import Protocol, TypedDict, runtime_checkable  # type: ignore # noqa
EOF
cat > airflow/stats.py <<EOF
from airflow.typing_compat import Protocol


class TimerProtocol(Protocol):
    """Type protocol for StatsLogger.timer"""

    def __enter__(self):
        ...

    def __exit__(self, exc_type, exc_value, traceback):
        ...

    def start(self):
        """Start the timer"""
        ...

    def stop(self, send=True):
        """Stop, and (by default) submit the timer to statsd"""
        ...
EOF

Run it:

sphinx-build -W --color -T -b html \
    -C \
    -D autoapi_keep_files=True \
    -D extensions=autoapi.extension \
    -D autoapi_dirs=airflow \
    . _build

Output:

airflow ❯ sphinx-build -W --color -T -b html -C -D autoapi_keep_files=True -D extensions=autoapi.extension -D autoapi_dirs=airflow  . _build
Running Sphinx v3.5.4
making output directory... done
[AutoAPI] Loading Data
[AutoAPI] Reading files... [100%] /home/ash/code/python/flask-sqlalchemy/tmp/airflow/typing_compat.py                                                                                                                                                                                                                                                         
[AutoAPI] Mapping Data

Traceback (most recent call last):
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/sphinx/cmd/build.py", line 279, in build_main
    args.tags, args.verbosity, args.jobs, args.keep_going)
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/sphinx/application.py", line 278, in __init__
    self._init_builder()
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/sphinx/application.py", line 337, in _init_builder
    self.events.emit('builder-inited')
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/sphinx/events.py", line 111, in emit
    results.append(listener.handler(self.app, *args))
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/autoapi/extension.py", line 101, in run_autoapi
    sphinx_mapper_obj.map(options=app.config.autoapi_options)
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/autoapi/mappers/python/mapper.py", line 243, in map
    self._resolve_placeholders()
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/autoapi/mappers/python/mapper.py", line 240, in _resolve_placeholders
    _resolve_module_placeholders(modules, module_name, visit_path, resolved)
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/autoapi/mappers/python/mapper.py", line 142, in _resolve_module_placeholders
    LOGGER.warning(msg)
  File "/usr/lib64/python3.7/logging/__init__.py", line 1734, in warning
    self.log(WARNING, msg, *args, **kwargs)
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/sphinx/util/logging.py", line 126, in log
    super().log(level, msg, *args, **kwargs)
  File "/usr/lib64/python3.7/logging/__init__.py", line 1766, in log
    self.logger.log(level, msg, *args, **kwargs)
  File "/usr/lib64/python3.7/logging/__init__.py", line 1444, in log
    self._log(level, msg, args, **kwargs)
  File "/usr/lib64/python3.7/logging/__init__.py", line 1514, in _log
    self.handle(record)
  File "/usr/lib64/python3.7/logging/__init__.py", line 1524, in handle
    self.callHandlers(record)
  File "/usr/lib64/python3.7/logging/__init__.py", line 1586, in callHandlers
    hdlr.handle(record)
  File "/usr/lib64/python3.7/logging/__init__.py", line 890, in handle
    rv = self.filter(record)
  File "/usr/lib64/python3.7/logging/__init__.py", line 751, in filter
    result = f.filter(record)
  File "/home/ash/.virtualenvs/airflow/lib/python3.7/site-packages/sphinx/util/logging.py", line 422, in filter
    raise exc
sphinx.errors.SphinxWarning: Cannot resolve import of airflow.typing_compat.Protocol in airflow.stats

Warning, treated as error:
Cannot resolve import of airflow.typing_compat.Protocol in airflow.stats

ashb avatar Apr 16 '21 20:04 ashb

This may not be a problem, and all I want is an option to not warn on imports that can't be resolved from outside "airflow."

ashb avatar Apr 16 '21 21:04 ashb

We recently added support for using suppress_warnings (#277). It doesn't differentiate between local and external imports though.

AWhetter avatar Apr 20 '21 18:04 AWhetter

Found this in poliastro as well. I confirm that suppress_warnings.append('autoapi.python_import_resolution') makes the warning go away, although it's less than ideal.

astrojuanlu avatar Dec 26 '21 18:12 astrojuanlu