Repository navigation
Closed
Labels
code review in progresscode has been provided that's in review as PR and/or gerritcode has been provided that's in review as PR and/or gerrittypingpep -484 typing issues. independent of "mypy"pep -484 typing issues. independent of "mypy"
Description
Ensure stubs packages are not installed
- No sqlalchemy stub packages is installed (both
sqlalchemy-stubsandsqlalchemy2-stubsare 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:
sqlalchemy/lib/sqlalchemy/inspection.py
Lines 106 to 111 in ac3deae
| @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]: ...Metadata
Metadata
Assignees
Labels
code review in progresscode has been provided that's in review as PR and/or gerritcode has been provided that's in review as PR and/or gerrittypingpep -484 typing issues. independent of "mypy"pep -484 typing issues. independent of "mypy"
Activity
0xamlab commentedon Oct 20, 2026
CaselIT commentedon Oct 20, 2026
The fix looks reasonable. There should already be a typing tests for inspect that can be extended
0xamlab commentedon Oct 21, 2026
CaselIT commentedon Oct 21, 2026
@0xamlab please stop posting spam
sqla-tester commentedon Oct 23, 2026
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
Fix Any overloads for inspect() raiseerr handling