summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/mypy
Commit message (Collapse)AuthorAgeFilesLines
* establish column_property and query_expression as readonly from a dc perspectiveMike Bayer2023-04-121-1/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | Fixed bug in ORM Declarative Dataclasses where the :func:`_orm.queryable_attribute` and :func:`_orm.column_property` constructs, which are documented as read-only constructs in the context of a Declarative mapping, could not be used with a :class:`_orm.MappedAsDataclass` class without adding ``init=False``, which in the case of :func:`_orm.queryable_attribute` was not possible as no ``init`` parameter was included. These constructs have been modified from a dataclass perspective to be assumed to be "read only", setting ``init=False`` by default and no longer including them in the pep-681 constructor. The dataclass parameters for :func:`_orm.column_property` ``init``, ``default``, ``default_factory``, ``kw_only`` are now deprecated; these fields don't apply to :func:`_orm.column_property` as used in a Declarative dataclasses configuration where the construct would be read-only. Also added read-specific parameter :paramref:`_orm.queryable_attribute.compare` to :func:`_orm.queryable_attribute`; :paramref:`_orm.queryable_attribute.repr` was already present. Added missing :paramref:`_orm.mapped_column.active_history` parameter to :func:`_orm.mapped_column` construct. Fixes: #9628 Change-Id: I2ab44d6b763b20410bd1ebb5ac949a6d223f1ce2
* mypy plugin fixesMike Bayer2023-01-182-8/+19
| | | | | | | | | | | | | | | | | | | Adjustments made to the mypy plugin to accommodate for some potential changes being made for issue #236 sqlalchemy2-stubs when using SQLAlchemy 1.4. These changes are being kept in sync within SQLAlchemy 2.0. The changes are also backwards compatible with older versions of sqlalchemy2-stubs. Fixed crash in mypy plugin which could occur on both 1.4 and 2.0 versions if a decorator for the :func:`_orm.registry.mapped` decorator were used that was referenced in an expression with more than two components (e.g. ``@Backend.mapper_registry.mapped``). This scenario is now ignored; when using the plugin, the decorator expression needs to be two components (i.e. ``@reg.mapped``). References: https://github.com/sqlalchemy/sqlalchemy2-stubs/issues/236 Fixes: #9102 Change-Id: Ieb1bf7bf8184645bcd43253e57f1c267b2640537
* happy new year 2023Mike Bayer2023-01-032-2/+2
| | | | Change-Id: I625af65b3fb1815b1af17dc2ef47dd697fdc3fb1
* Try running pyupgrade on the codeFederico Caselli2022-11-162-4/+3
| | | | | | | | command run is "pyupgrade --py37-plus --keep-runtime-typing --keep-percent-format <files...>" pyupgrade will change assert_ to assertTrue. That was reverted since assertTrue does not exists in sqlalchemy fixtures Change-Id: Ie1ed2675c7b11d893d78e028aad0d1576baebb55
* reorganize Mapped[] super outside of MapperPropertyMike Bayer2022-10-051-0/+44
| | | | | | | | | | | | | | | | | | | | | | | | | | We made all the MapperProperty classes a subclass of Mapped[] to allow declarative mappings to name Mapped[] on the left side. this was cheating a bit because MapperProperty is not actually a descriptor, and the mapping process replaces the object with InstrumentedAttribute at mapping time, which is the actual Mapped[] descriptor. But now in I6929f3da6e441cad92285e7309030a9bac4e429d we are considering making the "cheating" a little more extensive by putting DynamicMapped / WriteOnlyMapped in Relationship's hierarchy, which need a flat out "type: ignore" to work. Instead of pushing more cheats into the core classes, move out the "Declarative"-facing versions of these classes to be typing only: Relationship, Composite, Synonym, and MappedSQLExpression added for ColumnProperty. Keep the internals expressed on the old names, RelationshipProperty, CompositeProperty, SynonymProperty, ColumnProprerty, which will remain "pure" with fully correct typing. then have the typing only endpoints be where the "cheating" and "type: ignores" have to happen, so that these are more or less slightly better forms of "Any". Change-Id: Ied7cc11196c9204da6851f49593d1b1fd2ef8ad8
* Update to flake8 5.Federico Caselli2022-07-311-2/+3
| | | | Change-Id: I5a241a70efba68bcea9819ddce6aebc25703e68d
* Support lambda expression in mypy pluginCyril Chapellier2022-07-021-0/+5
| | | | | | | Avoid `error: INTERNAL ERROR` when the default is a lambda Fixes: #8196 Change-Id: I7346c693519b024c56156db6f4ffc9a45bb748d3
* inline mypy config; files ignoring type errors for the momentMike Bayer2022-04-282-1/+8
| | | | | | | | | | | | | | | | | | | to simplify pyproject.toml change the remaining files that aren't going to be typed on this first pass (unless of course someone wants to type some of these) to include # mypy: ignore-errors. for the moment, only a handful of ORM modules are to have more type checking implemented. It's important that ignore-errors is used and not "# type: ignore", as in the latter case, mypy doesn't even read the existing types in the file, which makes it impossible to type any files that refer to those modules at all. to simplify ongoing typing work use inline mypy config for remaining files that are "done" for now, indicating the level of type checking they currently have. Change-Id: I98669c1a305c2f0adba85d10b5425541f3fe9533
* pep484 - SQL internalsMike Bayer2022-03-241-1/+1
| | | | | | | non-strict checking for mostly internal or semi-internal code Change-Id: Ib91b47f1a8ccc15e666b94bad1ce78c4ab15b0ec
* pep-484 for poolMike Bayer2022-02-171-6/+6
| | | | | | | | | | | | | | | | | also extends into some areas of utils, events and others as needed. Formalizes a public hierarchy for pool API, with ManagesConnection -> PoolProxiedConnection / ConnectionPoolEntry for connectionfairy / connectionrecord, which are now what's exposed in the event API and other APIs. all public API docs moved to the new objects. Corrects the mypy plugin's check for sqlalchemy-stubs not being insatlled, which has to be imported using the dash in the name to be effective. Change-Id: I16c2cb43b2e840d28e70a015f370a768e70f3581
* pep-484 for sqlalchemy.event; use future annotationsMike Bayer2022-02-156-0/+12
| | | | | | | | | | __future__.annotations mode allows us to use non-string annotations for argument and return types in most cases, but more importantly it removes a large amount of runtime overhead that would be spent in evaluating the annotations. Change-Id: I2f5b6126fe0019713fc50001be3627b664019ede References: #6810
* establish mypy / typing approach for v2.0Mike Bayer2022-02-136-40/+138
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | large patch to get ORM / typing efforts started. this is to support adding new test cases to mypy, support dropping sqlalchemy2-stubs entirely from the test suite, validate major ORM typing reorganization to eliminate the need for the mypy plugin. * New declarative approach which uses annotation introspection, fixes: #7535 * Mapped[] is now at the base of all ORM constructs that find themselves in classes, to support direct typing without plugins * Mypy plugin updated for new typing structures * Mypy test suite broken out into "plugin" tests vs. "plain" tests, and enhanced to better support test structures where we assert that various objects are introspected by the type checker as we expect. as we go forward with typing, we will add new use cases to "plain" where we can assert that types are introspected as we expect. * For typing support, users will be much more exposed to the class names of things. Add these all to "sqlalchemy" import space. * Column(ForeignKey()) no longer needs to be `@declared_attr` if the FK refers to a remote table * composite() attributes mapped to a dataclass no longer need to implement a `__composite_values__()` method * with_variant() accepts multiple dialect names Change-Id: I22797c0be73a8fbbd2d6f5e0c0b7258b17fe145d Fixes: #7535 Fixes: #7551 References: #6810
* fix(mypy): Placeholder vars should have fullname set.Jaen Saul2022-01-151-0/+1
| | | | | | | | | | | Otherwise the dmypy daemon will crash on an incremental re-run. Fixes: #7347 Closes: #7348 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7348 Pull-request-sha: 79eefa3417e09a8f9defaeafdb9f76d323385238 Change-Id: Id2133e837ee5dcf43461af51458e296353bdad6c
* Initial ORM typing layoutMike Bayer2022-01-142-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | introduces: 1. new mapped_column() helper 2. DeclarativeBase helper 3. declared_attr has been re-typed 4. rework of Mapped[] to return InstrumentedAtribute for class get, so works without Mapped itself having expression methods 5. ORM constructs now generic on [_T] also includes some early typing work, most of which will be in later commits: 1. URL and History become typing.NamedTuple 2. come up with type-checking friendly way of type checking cy extensions, where type checking will be applied to the py versions, just needed to come up with a succinct conditional pattern for the imports References: #6810 References: #7535 References: #7562 Change-Id: Ie5d9a44631626c021d130ca4ce395aba623c71fb
* Merge "Fix various source comment/doc typos" into mainmike bayer2022-01-071-1/+1
|\
| * Fix various source comment/doc typosluz paz2021-12-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Description Found via `codespell -q 3 -L ba,crate,datas,froms,gord,hist,inh,nd,selectin,strat,ue` Also added codespell to the pep8 tox env ### Checklist This pull request is: - [x] A documentation / typographical error fix - Good to go, no issue or tests are needed Closes: #7338 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7338 Pull-request-sha: 0deac2219396bc0eba7da53eb3a80932edbf2dd7 Change-Id: Icd61db31c8dc655d4a39d8a304194804d08555fe
* | Update Black's target-version to py37Hugo van Kemenade2022-01-051-2/+2
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | <!-- Provide a general summary of your proposed changes in the Title field above --> ### Description <!-- Describe your changes in detail --> Black's `target-version` was still set to `['py27', 'py36']`. Set it to `[py37]` instead. Also update Black and other pre-commit hooks and re-format with Black. ### Checklist <!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once) --> This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [ ] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. **Have a nice day!** Closes: #7536 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7536 Pull-request-sha: b3aedf5570d7e0ba6c354e5989835260d0591b08 Change-Id: I8be85636fd2c9449b07a8626050c8bd35bd119d5
* use fully qualified, locatable names for all use of api.named_type()Mike Bayer2021-12-225-11/+20
| | | | | | | | | | | | Fixed mypy regression where the release of mypy 0.930 added additional internal checks to the format of "named types", requiring that they be fully qualified and locatable. This broke the mypy plugin for SQLAlchemy, raising an assertion error, as there was use of symbols such as ``__builtins__`` and other un-locatable or unqualified names that previously had not raised any assertions. Fixes: #7496 Change-Id: I037680606a1d51158ef6503508ec76c5d5adc946
* handle dunder names in @declared_attr separatelyMike Bayer2021-11-172-1/+19
| | | | | | | | | | | | | | | | Fixed Mypy crash which would occur when using Mypy plugin against code which made use of :class:`_orm.declared_attr` methods for non-mapped names like ``__mapper_args__``, ``__table_args__``, or other dunder names, as the plugin would try to interpret these as mapped attributes which would then be later mis-handled. As part of this change, the decorated function is still converted by the plugin into a generic assignment statement (e.g. ``__mapper_args__: Any``) so that the argument signature can continue to be annotated in the same way one would for any other ``@classmethod`` without Mypy complaining about the wrong argument type for a method that isn't explicitly ``@classmethod``. Fixes: #7321 Change-Id: I55656e867876677c5c55143449db371344be8600
* fix(6435): support `MemberExpr` for enum column declarationHiroshi Ogawa2021-10-051-1/+1
| | | | | | | | | | | | | Fixed issue in mypy plugin to improve upon some issues detecting ``Enum()`` SQL types containing custom Python enumeration classes. Pull request courtesy Hiroshi Ogawa. Fixes: #6435 Closes: #7048 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7048 Pull-request-sha: 59f5c89688792f6af3b07488d5cf97f8f2e964dc Change-Id: I05adbec74ceac1ecfdc5a242bfe7aa4b2eb805e4
* dont assume argument lists for column propertyMike Bayer2021-08-272-7/+27
| | | | | | | | Fixed issue where mypy plugin would crash when interpreting a ``query_expression()`` construct. Fixes: #6950 Change-Id: Ic1f28d135bf6eb05c92061430c0d5a3663b804ef
* ensure "sqlalchemy" info set for all considered classesMike Bayer2021-08-252-0/+7
| | | | | | | | | Fixed issue in mypy plugin where columns on a mixin would not be correctly interpreted if the mapped class relied upon a ``__tablename__`` routine that came from a superclass. Fixes: #6937 Change-Id: I74aed4862d0545008ee67f781aaa794ab6866926
* Refactor mypy pluginBryan Forbes2021-08-076-312/+410
| | | | | | | | | | A major refactor of the mypy plugin Closes: #6764 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/6764 Pull-request-sha: 3e2295b2da7b57a6669f26db0df78f6409934184 Change-Id: I067d56dcfbc998ddd1b22a448f756859428b9e31
* Replace all http:// links to https://Federico Caselli2021-07-045-5/+5
| | | | | | Also replace http://pypi.python.org/pypi with https://pypi.org/project Change-Id: I84b5005c39969a82140706472989f2a30b0c7685
* accommodate no cls info found in _scan_declarativeMike Bayer2021-06-212-1/+6
| | | | | | | | | Fixed issue in mypy plugin where class info for a custom declarative base would not be handled correctly on a cached mypy pass, leading to an AssertionError being raised. Fixes: #6476 Change-Id: If78340673e6a4d16d8f7cf787ce3bdb02c8bd47b
* Re-infer statements that got more specific on subsequent passMike Bayer2021-04-203-32/+103
| | | | | | | | | | | | | | | | | | | | | | | | Fixed issue where mypy plugin would not correctly interpret an explicit :class:`_orm.Mapped` annotation in conjunction with a :func:`_orm.relationship` that refers to a class by string name; the correct annotation would be downgraded to a less specific one leading to typing errors. The thing figured out here is that after we've already scanned a class in the semanal stage and created DeclClassApplied, when we are called again with that same DeclClassApplied, for this specific kind of case we actually now have *better* types than we did before, where the left side that looked like List?[Address?] now seems to say builtins.list[official.module.Address] - so let's take the right side expression again, this time embedded in our Mapped._empty_constructor() expression, and run the infer all over again just like mypy would. Just not setting the "wrong" type here fixed the test cases but by re-applying the whole infer we get the correct Mapped[] on the left side too. Fixes: #6255 Change-Id: Iafe7254374f685a8458c7a1db82aafc2ed6d0232
* Fix OrderingList handlingBryan Forbes2021-04-131-0/+22
| | | | | | | | Revised the fix for ``OrderingList`` from version 1.4.7 which was testing against the incorrect API. Fixes: #6205 Change-Id: I1d3f8b6534b70ae000294c2a67f08117cb77ee99
* Update mypy plugin to conform to strict modeBryan Forbes2021-04-126-215/+382
| | | | Change-Id: I09a3df5af2f2d4ee34d8d72c3dedc4f236df8eb1
* Update mypy plugin to only use public plugin APIBryan Forbes2021-04-096-35/+40
| | | | Change-Id: Id7f4e4a39e17c1b6ec3c754e2fc5c6ba4b437c38
* Support `TypeDecorator` subclasses in `Column()` declarationsBryan Forbes2021-04-081-6/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | <!-- Provide a general summary of your proposed changes in the Title field above --> ### Description Currently, the plugin resolves `TypeDecorator` subclasses in `Column()` declarations to `Mapping[_T]` instead of the correct type in the class declaration. ### Checklist <!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once) --> This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [X] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #<issue number>` in the commit message - please include tests. **Have a nice day!** Closes: #6223 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/6223 Pull-request-sha: 5136bc4b6333e868cc47f1b2dcc58716a40cadca Change-Id: Idc865bf7320f8ea3054c28dea095b693fe112753
* Accommodate for callable fns for collection_classMike Bayer2021-04-061-5/+28
| | | | | | | | | | Fixed issue where the Mypy plugin would fail to interpret the "collection_class" of a relationship if it were a callable and not a class. Also improved type matching and error reporting for collection-oriented relationships. Fixes: #6205 Change-Id: If3cb0defd4d7336e06a3bb3a3e8d59ea34b4c98d
* Adjust for mypy incremental behaviorsMike Bayer2021-04-056-654/+831
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Applied a series of refactorings and fixes to accommodate for Mypy "incremental" mode across multiple files, which previously was not taken into account. In this mode the Mypy plugin has to accommodate Python datatypes expressed in other files coming in with less information than they have on a direct run. Additionally, a new decorator :func:`_orm.declarative_mixin` is added, which is necessary for the Mypy plugin to be able to definifitely identify a Declarative mixin class that is otherwise not used inside a particular Python file. discussion: With incremental / deserialized mypy runs, it appears that when we look at a base class that comes from another file, cls.info is set to a special undefined node that matches CLASSDEF_NO_INFO, and we otherwise can't touch it without crashing. Additionally, sometimes cls.defs.body is present but empty. However, it appears that both of these cases can be sidestepped, first by doing a lookup() for the type name where we get a SymbolTableNode that then has the TypeInfo we wanted when we tried touching cls.info, and then however we got the TypeInfo, if cls.defs.body is empty we can just look in the names to get at the symbols for that class; we just can't access AssignmentStmts, but that's fine because we just need the information for classes we aren't actually type checking. This work also revealed there's no easy way to detect a mixin class so we just create a new decorator to mark that. will make code look better in any case. Fixes: #6147 Change-Id: Ia8fac8acfeec931d8f280491cffc5c6cb4a1204e
* Add DeclarativeMeta to globalsMike Bayer2021-03-313-31/+67
| | | | | | | | | | | | | | | | Fixed issue in mypy plugin where newly added support for :func:`_orm.as_declarative` needed to more fully add the ``DeclarativeMeta`` class to the mypy interpreter's state so that it does not result in a name not found error; additionally improves how global names are setup for the plugin including the ``Mapped`` name. Introduces directory oriented testing as well, where a full set of files will be copied, mypy runs, then zero or more patches are applied and mypy is run again, to fully test incremental behaviors. Fixes: sqlalchemy/sqlalchemy2-stubs/#14 Change-Id: Ide785c07e19ba0694e8cf6f91560094ecb182016
* support as_declarative, as_declarative_baseMike Bayer2021-03-252-39/+88
| | | | | | | | Added support for the Mypy extension to correctly interpret a declarative base class that's generated using the :func:`_orm.as_declarative` function as well as the :meth:`_orm.registry.as_declarative_base` method. Change-Id: I227f4abebe157a7df3f8772893bbea6669cc8555
* Add support for Boolean, EnumMike Bayer2021-03-211-12/+30
| | | | | | | | | | Fixed bug in Mypy plugin where the Python type detection for the :class:`_sqltypes.Boolean` column type would produce an exception; additionally implemented support for :class:`_sqltypes.Enum`, including detection of a string-based enum vs. use of Python ``enum.Enum``. Fixes: #6109 Change-Id: I25e546ea2f50d90be2d6fec303976d82849a3d31
* check for MemberExpr looking for column argumentMike Bayer2021-03-181-1/+1
| | | | | | | | | Fixed issue in MyPy extension which crashed on detecting the type of a :class:`.Column` if the type were given with a module prefix like ``sa.Integer()``. Fixes: sqlalchemy/sqlalchemy2-stubs/#2 Change-Id: I71f53a6ced501ae144e28ce255cf3f50ea2b2e84
* Implement Mypy pluginMike Bayer2021-03-135-0/+1478
Rudimentary and experimental support for Mypy has been added in the form of a new plugin, which itself depends on new typing stubs for SQLAlchemy. The plugin allows declarative mappings in their standard form to both be compatible with Mypy as well as to provide typing support for mapped classes and instances. Fixes: #4609 Change-Id: Ia035978c02ad3a5c0e5b3c6c30044dd5a3155170