]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Remove unused TypeVars and compat import from typing modules
authorShamil Abdulaev <ashm.tech@proton.me>
Thu, 23 Apr 2026 17:09:07 +0000 (13:09 -0400)
committersqla-tester <sqla-tester@sqlalchemy.org>
Thu, 23 Apr 2026 17:09:07 +0000 (13:09 -0400)
## Summary

Removes 4 dead symbols from SQLAlchemy's typing modules. Each symbol has **zero references** across `lib/`, `test/`, `doc/`, `tools/`, and `examples/` — verified by grep for direct references, string forward references, and `getattr`-style dynamic access.

## Changes

**`lib/sqlalchemy/util/typing.py`**
- Remove `from . import compat` — unused module import; the only occurrence of `compat` in the file is in a code comment on line 221
- Remove `_KT_co = TypeVar("_KT_co", covariant=True)` — unused TypeVar
- Remove `_KT_contra = TypeVar("_KT_contra", contravariant=True)` — unused TypeVar

**`lib/sqlalchemy/orm/_typing.py`**
- Remove `_T_co = TypeVar("_T_co", bound=Any, covariant=True)` — orphaned; `orm.base` imports `_T_co` from `sql._typing` instead, and other orm modules (`interfaces.py`, `attributes.py`) define their own local `_T_co`

Total: **-7 lines**, no additions.

## Verification

For each removed symbol:

| Symbol | Repo-wide grep result |
|---|---|
| `compat` (in `util/typing.py`) | 0 references to `compat.` or `util.typing.compat` outside the import itself |
| `_KT_co` | 1 hit total (the removed definition line) |
| `_KT_contra` | 1 hit total (the removed definition line) |
| `_T_co` (in `orm/_typing.py`) | No `from sqlalchemy.orm._typing import _T_co` or `from ..orm._typing import _T_co` anywhere; all consumers import from `sql._typing` or define locally |

Additionally checked:
- No `__all__` references
- No string forward references (e.g. `"_KT_co"`)
- No `getattr(..., "_T_co")` or similar dynamic access
- No re-exports via `util/__init__.py` or any other module
- `test/base/test_typing_utils.py` (which imports `util.typing as sa_typing`) does not touch any of these names

## Test plan

- [x] `pre-commit` hooks pass (black, zimports, flake8)
- [x] `import sqlalchemy`, `sqlalchemy.orm`, `sqlalchemy.util.typing`, `sqlalchemy.orm._typing`, `sqlalchemy.sql._typing`, `sqlalchemy.dialects._typing` — all import cleanly
- [x] Broad import smoke test: `Mapped`, `DeclarativeBase`, `relationship`, `Session`, `Mapper`, `mapped_column`, `Column`, `select`, `create_engine`, `MetaData`, `QueuePool`, `hybrid_property`, `association_proxy` — all import cleanly
- [x] Full `tox` / test suite — would appreciate CI confirmation

## Not included (intentionally)

- `LiteralString` re-export in `util/typing.py:54` — has 0 in-tree consumers but is an explicit `as X as X` public re-export; kept for API stability
- `is_origin_of` rename to `_is_origin_of` — used only internally by `is_union`, but renaming is a minor breaking change not worth the churn

Closes: #13244
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13244
Pull-request-sha: 8242f373685c35264ffd52071345a6dfce855277

Change-Id: Iae389447ac46599df27ab9776ff5ad19dfef4e2c

lib/sqlalchemy/orm/_typing.py
lib/sqlalchemy/util/typing.py

index 2e8c49320a40efa7fc5974ec610093a68d08d13e..ab9518beb48ef66355871fe3201dfd9c1b51a6f4 100644 (file)
@@ -50,9 +50,6 @@ if TYPE_CHECKING:
 
 _T = TypeVar("_T", bound=Any)
 
-
-_T_co = TypeVar("_T_co", bound=Any, covariant=True)
-
 _O = TypeVar("_O", bound=object)
 """The 'ORM mapped object' type.
 
index 01bf0a7b3a49acfb0141ae20502935319890ab1d..ef5b171b0a4c716fc445bc581ad434fe2ff6ccdd 100644 (file)
@@ -39,8 +39,6 @@ from typing import Union
 
 import typing_extensions
 
-from . import compat
-
 if True:  # zimports removes the tailing comments
     from typing_extensions import (
         dataclass_transform as dataclass_transform,  # 3.11,
@@ -57,8 +55,6 @@ if True:  # zimports removes the tailing comments
 
 _T = TypeVar("_T", bound=Any)
 _KT = TypeVar("_KT")
-_KT_co = TypeVar("_KT_co", covariant=True)
-_KT_contra = TypeVar("_KT_contra", contravariant=True)
 _VT = TypeVar("_VT")
 _VT_co = TypeVar("_VT_co", covariant=True)