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

Crash - ParentMissingError and strange instance of wrong-import-position warning #9446

Closed
widal001 opened this issue Feb 18, 2024 · 2 comments
Labels
Duplicate 🐫 Duplicate of an already existing issue

Comments

@widal001
Copy link

widal001 commented Feb 18, 2024

Bug description

When parsing the following a.py:

"""Create base models that other models can inherit from."""

from uuid import UUID

from sqlalchemy import DateTime
from sqlalchemy.orm import (
    DeclarativeBase,
    Mapped,
    declared_attr,
    mapped_column,
)
from sqlalchemy.sql import functions


class UUIDAuditBase(DeclarativeBase):
    """Base db model that includes id, created_at, and update_at."""

    id: Mapped[UUID] = mapped_column(primary_key=True)
    created_at: Mapped[DateTime] = mapped_column(
        DateTime(timezone=True),
        default=functions.now(),
    )
    updated_at: Mapped[DateTime] = mapped_column(
        DateTime(timezone=True),
        default=functions.now(),
        onupdate=functions.now(),
    )

    @declared_attr.directive
    @classmethod
    def __tablename__(cls) -> str:
        """Set default table name as the lowercase version of the class name."""
        return cls.__name__.lower()

Command used

poetry run pylint src tests

Pylint output

pylint crashed with a ``AstroidError`` and with the following stacktrace:
Traceback (most recent call last):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 788, in _lint_file
    check_astroid_module(module)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1017, in check_astroid_module
    retval = self._check_astroid_module(
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 1069, in _check_astroid_module
    walker.walk(node)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 94, in walk
    self.walk(child)
  [Previous line repeated 1 more time]
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/utils/ast_walker.py", line 91, in walk
    callback(astroid)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/checkers/base/basic_checker.py", line 705, in visit_call
    if utils.is_terminating_func(node):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/checkers/utils.py", line 2160, in is_terminating_func
    for inferred in node.func.infer():
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/decorators.py", line 90, in inner
    yield next(generator)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/decorators.py", line 49, in wrapped
    for res in _func(node, context, **kwargs):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/nodes/node_classes.py", line 1090, in _infer_attribute
    for owner in node.expr.infer(context):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 169, in infer
    for i, result in enumerate(self._infer(context=context, **kwargs)):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/decorators.py", line 90, in inner
    yield next(generator)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/decorators.py", line 49, in wrapped
    for res in _func(node, context, **kwargs):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/nodes/node_classes.py", line 1107, in _infer_attribute
    yield from owner.igetattr(node.attrname, context)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/nodes/scoped_nodes/scoped_nodes.py", line 2498, in igetattr
    first_scope = first_attr.scope()
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/astroid/nodes/node_ng.py", line 327, in scope
    raise ParentMissingError(target=self)
astroid.exceptions.ParentMissingError: Parent not found on <Const.str l.0 at 0x10e74a590>.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 752, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/Users/williamdaly/Documents/cofundable/api/.venv/lib/python3.10/site-packages/pylint/lint/pylinter.py", line 790, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

I also got some unexpected and seemingly conflicting errors in a different file that inherits from the UUIDAuditBase class in base.py:

************* Module src.cofundable.models.base
src/cofundable/models/base.py:1:0: F0002: src/cofundable/models/base.py: Fatal error while checking 'src/cofundable/models/base.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/Users/williamdaly/Library/Caches/pylint/pylint-crash-2024-02-17-23-34-19.txt'. (astroid-error)
************* Module src.cofundable.models.tag
src/cofundable/models/tag.py:3:0: C0413: Import "from __future__ import annotations" should be placed at the top of the module (wrong-import-position)
src/cofundable/models/tag.py:5:0: C0413: Import "from typing import TYPE_CHECKING, Optional" should be placed at the top of the module (wrong-import-position)
src/cofundable/models/tag.py:10:0: C0413: Import "from sqlalchemy.orm import relationship" should be placed at the top of the module (wrong-import-position)
src/cofundable/models/tag.py:12:0: C0413: Import "from cofundable.models.associations import cause_tag_table" should be placed at the top of the module (wrong-import-position)
src/cofundable/models/tag.py:13:0: C0413: Import "from cofundable.models.base import Mapped, UUIDAuditBase" should be placed at the top of the module (wrong-import-position)
src/cofundable/models/tag.py:3:0: C0411: standard import "from __future__ import annotations" should be placed before "from sqlalchemy import DateTime" (wrong-import-order)
src/cofundable/models/tag.py:5:0: C0411: standard import "from typing import TYPE_CHECKING, Optional" should be placed before "from sqlalchemy import DateTime" (wrong-import-order)
src/cofundable/models/tag.py:10:0: C0412: Imports from package sqlalchemy are not grouped (ungrouped-imports)
src/cofundable/models/tag.py:12:0: C0412: Imports from package cofundable are not grouped (ungrouped-imports)

This is the module tag.py which was causing the second set of unexpected errors. It's worth noting that this module doesn't trigger any errors on Ruff's isort rule set.

"""Create an ORM for the tag table in the database."""

from __future__ import annotations

from typing import TYPE_CHECKING, Optional

from sqlalchemy.orm import relationship

from cofundable.models.associations import cause_tag_table
from cofundable.models.base import Mapped, UUIDAuditBase

if TYPE_CHECKING:
    from cofundable.models.cause import Cause


class Tag(UUIDAuditBase):
    """Store information related to a cause."""

    name: Mapped[str]
    description: Mapped[Optional[str]]  # noqa: UP007

    causes: Mapped[list[Cause]] = relationship(
        secondary=cause_tag_table,
        back_populates="tags",
    )

Expected behavior

No crash.

Pylint version

pylint 3.0.3
astroid 3.0.2
Python 3.10.6 (main, Aug 12 2022, 10:46:27) [Clang 13.1.6 (clang-1316.0.21.2.3)]

OS / Environment

darwin (Darwin)

Additional dependencies

sqlalchemy="2.0.25"

@widal001 widal001 changed the title Crash - ParentMissingError() Crash - ParentMissingError and strange instance of wrong-import-position warning Feb 18, 2024
@widal001
Copy link
Author

This may be related to #8920 or #7680

@widal001
Copy link
Author

Closing as a duplicate of #7680

@widal001 widal001 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2024
@jacobtylerwalls jacobtylerwalls added the Duplicate 🐫 Duplicate of an already existing issue label Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🐫 Duplicate of an already existing issue
Projects
None yet
Development

No branches or pull requests

2 participants