Skip to content

Inspect overloads for Any handle raiseerr wrong #13704

Closed

Description

@lenard-mosys

Ensure stubs packages are not installed

  • No sqlalchemy stub packages is installed (both sqlalchemy-stubs and sqlalchemy2-stubs are not compatible with v2)

Verify if the api is typed

  • The api is not in a module listed in #6810 so it should pass type checking

Describe the typing issue

The following overloads handle raiseerr wrong:

@overload
def inspect(subject: Any, raiseerr: Literal[False] = ...) -> Optional[Any]: ...
@overload
def inspect(subject: Any, raiseerr: bool = True) -> Any: ...

To Reproduce

from typing import Protocol, reveal_type

from sqlalchemy import inspect


class Named(Protocol):
    name: str


def omitted(obj: Named) -> None:
    # raiseerr defaults to True, so inspect() raises instead of returning None
    reveal_type(inspect(obj))  # Any | None  (expected: Any)
    inspect(obj).identity  # false positive: Item "None" of "Any | None" has no attribute "identity"


def non_literal_bool(obj: Named, raiseerr: bool) -> None:
    # raiseerr may be False at runtime, so inspect() may return None
    reveal_type(inspect(obj, raiseerr=raiseerr))  # Any  (expected: Any | None)
    inspect(obj, raiseerr=raiseerr).identity  # false negative: no error, but AttributeError at runtime


if __name__ == "__main__":

    class Plain:
        name = "plain"

    non_literal_bool(Plain(), raiseerr=False)

Error

inspect_repro.py:12: note: Revealed type is "Any | None"
inspect_repro.py:13: error: Item "None" of "Any | None" has no attribute "identity"  [union-attr]
inspect_repro.py:18: note: Revealed type is "Any"
Found 1 error in 1 file (checked 1 source file)

Versions

  • OS: Ubuntu 24.04 in WSL
  • SQLAlchemy 2.0.54
  • mypy 1.18.2
  • Python 3.12

Additional context

Possible fix:

Use the following overloads instead:

@overload
def inspect(subject: Any, raiseerr: Literal[True] = ...) -> Any: ...
@overload
def inspect(subject: Any, raiseerr: bool) -> Optional[Any]: ...

Activity

  1. added
    requires triageNew issue that requires categorization
    typingpep -484 typing issues. independent of "mypy"
    on Oct 20, 2026
  2. 0xamlab commented on Oct 20, 2026

    @0xamlab
  3. added
    open for pull requestsMaintainers have approved this issue for an outside pull request
    and removed
    requires triageNew issue that requires categorization
    on Oct 20, 2026
  4. CaselIT commented on Oct 20, 2026

    @CaselIT
    Member

    The fix looks reasonable. There should already be a typing tests for inspect that can be extended

  5. added
    code review in progresscode has been provided that's in review as PR and/or gerrit
    and removed
    open for pull requestsMaintainers have approved this issue for an outside pull request
    on Oct 21, 2026
  6. 0xamlab commented on Oct 21, 2026

    @0xamlab
  7. CaselIT commented on Oct 21, 2026

    @CaselIT
    Member

    @0xamlab please stop posting spam

  8. sqla-tester commented on Oct 23, 2026

    @sqla-tester
    Collaborator

    Prince Jain has proposed a fix for this issue in the main branch:

    Fix Any overloads for inspect() raiseerr handling https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/7122

  9. added a commit that references this issue on Oct 29, 2026
    47615a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    code review in progresscode has been provided that's in review as PR and/or gerrittypingpep -484 typing issues. independent of "mypy"

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions