Skip to content

Commit 47615a8

Browse files
princejain756sqla-tester
authored andcommitted
Fix Any overloads for inspect() raiseerr handling
### Fixes Fixes: #13704 ### Description Update the fallback `@overload` signatures for `inspect(subject: Any, ...)` in `lib/sqlalchemy/inspection.py` so the default `raiseerr: Literal[True] = ... -> Any` overload precedes `raiseerr: bool -> Optional[Any]`: - Calling `inspect(obj)` with `raiseerr` omitted or `True` now resolves to `Any` instead of `Any | None`. - Calling `inspect(obj, raiseerr=flag)` with `False` or a `bool` variable now resolves to `Optional[Any]` (`Any | None`). - Extend `test/typing/plain_files/inspection_inspect.py` to cover omitted `raiseerr`, literal booleans, and a `bool` parameter on a `Protocol`-typed subject. Closes: #13710 Pull-request: #13710 Pull-request-sha: c5097c9 Change-Id: I128b4c234c72f19cdf4590b9d6a34f201fc7adc8
1 parent 10f4d1e commit 47615a8

5 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎lib/sqlalchemy/inspection.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ def inspect(subject: Inspectable[_IN], raiseerr: bool = True) -> _IN: ...
104104

105105

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

109109

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

113113

114114
def inspect(subject: Any, raiseerr: bool = True) -> Any:

‎lib/sqlalchemy/orm/query.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ def with_parent(
13071307
for prop in mapper.iterate_properties:
13081308
if (
13091309
isinstance(prop, relationships.RelationshipProperty)
1310-
and prop.mapper is entity_zero.mapper # type: ignore[union-attr] # noqa: E501
1310+
and prop.mapper is entity_zero.mapper
13111311
):
13121312
property = prop # type: ignore[assignment] # noqa: A001
13131313
break
@@ -1316,7 +1316,7 @@ def with_parent(
13161316
"Could not locate a property which relates instances "
13171317
"of class '%s' to instances of class '%s'"
13181318
% (
1319-
entity_zero.mapper.class_.__name__, # type: ignore[union-attr] # noqa: E501
1319+
entity_zero.mapper.class_.__name__,
13201320
instance.__class__.__name__,
13211321
)
13221322
)
@@ -1325,7 +1325,7 @@ def with_parent(
13251325
with_parent(
13261326
instance,
13271327
property, # type: ignore[arg-type]
1328-
entity_zero.entity, # type: ignore[union-attr]
1328+
entity_zero.entity,
13291329
)
13301330
)
13311331

‎lib/sqlalchemy/orm/relationships.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def adapt_to_entity(
667667

668668
def _memoized_attr_entity(self) -> _InternalEntityType[_PT]:
669669
if self._of_type:
670-
return inspect(self._of_type) # type: ignore[return-value]
670+
return inspect(self._of_type) # type: ignore[no-any-return]
671671
else:
672672
return self.prop.entity
673673

‎lib/sqlalchemy/orm/strategy_options.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,10 +2404,7 @@ def _parse_attr_argument(
24042404
24052405
"""
24062406
try:
2407-
# TODO: need to figure out this None thing being returned by
2408-
# inspect(), it should not have None as an option in most cases
2409-
# if at all
2410-
insp: InspectionAttr = inspect(attr) # type: ignore[assignment]
2407+
insp: InspectionAttr = inspect(attr)
24112408
except sa_exc.NoInspectionAvailable as err:
24122409
raise sa_exc.ArgumentError(
24132410
"expected ORM mapped attribute for loader strategy argument"

‎test/typing/plain_files/inspection_inspect.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from typing import Any
22
from typing import assert_type
33
from typing import List
4+
from typing import Optional
5+
from typing import Protocol
46

57
from sqlalchemy import create_engine
68
from sqlalchemy import inspect
@@ -61,3 +63,20 @@ class B(BaseNoMeta):
6163

6264
with e.connect() as conn:
6365
inspect(conn).get_table_names()
66+
67+
68+
class Named(Protocol):
69+
name: str
70+
71+
72+
def check_any_subject(obj: Named, flag: bool) -> None:
73+
assert_type(inspect(obj), Any)
74+
assert_type(inspect(obj, True), Any)
75+
assert_type(inspect(obj, raiseerr=True), Any)
76+
assert_type(inspect(obj, False), Optional[Any])
77+
assert_type(inspect(obj, raiseerr=False), Optional[Any])
78+
assert_type(inspect(obj, flag), Optional[Any])
79+
assert_type(inspect(obj, raiseerr=flag), Optional[Any])
80+
inspect(obj).identity
81+
# EXPECTED_MYPY: Item "None" of "Any | None" has no attribute "identity"
82+
inspect(obj, raiseerr=flag).identity

0 commit comments

Comments
 (0)