Open lukasjuhrich opened 2 years ago
Just because I don't know where else to write this, the following is worth noting about how to type the SQLAlchemy models:
Above, the ultimate class-level datatypes of id, name and other_name will be introspected as Mapped[Optional[int]], Mapped[Optional[str]] and Mapped[Optional[str]]. The types are by default always considered to be Optional, even for the primary key and non-nullable column. The reason is because while the database columns “id” and “name” can’t be NULL, the Python attributes id and name most certainly can be None without an explicit constructor:
(from the docs)
So, fundamentally, SQLAlchemy/mypy is not able to distinguish between
User
User
fetched from a database.This discrepancy is what statically typed languages usually avoid with the builder pattern.
Also relevant: https://mypy.readthedocs.io/en/stable/common_issues.html
See also these notes
web
enabled by #661
Many bugs in the past could have been avoided by some simple type checking. This is a Meta-Issue tracking the progress and to use as a reference in commits which extend our typing efforts.
Sub-Issues / milestones
557
mypy
pass on every file (null checks disabled)pycroft.lib
pycroft.model
pycroft.helpers
pycroft.helpers.date
pycroft.helpers.errorcode
pycroft.helpers.facilities
pycroft.helpers.i18n
pycroft.helpers.interval
pycroft.helpers.net
pycroft.helpers.printing
pycroft.helpers.task
pycroft.helpers.user
pycroft.helpers.utc
web
ldap_sync
pycroft.lib
pycroft.lib.address
pycroft.lib.exc
pycroft.lib.facilities
pycroft.lib.finance
pycroft.lib.host
pycroft.lib.infrastructure
pycroft.lib.logging
pycroft.lib.mail
pycroft.lib.membership
pycroft.lib.net
pycroft.lib.search
pycroft.lib.stats
pycroft.lib.swdd
pycroft.lib.task
pycroft.lib.traffic
pycroft.lib.user
pycroft.lib.user_deletion
pycroft.model
pycroft.helpers
pycroft.helpers.date
pycroft.helpers.errorcode
pycroft.helpers.facilities
pycroft.helpers.i18n
pycroft.helpers.interval
pycroft.helpers.net
pycroft.helpers.printing
pycroft.helpers.task
pycroft.helpers.user
pycroft.helpers.utc
web
ldap_sync
mypy
pass with null checks (i.e.no_strict_optional=false
) (if viable; might be quite a hassle)pycroft.lib
pycroft.model
web
MoneyAmount
NewType
for the sqlalchemyMoney
type decorator to not just haveDecimal
Acceptance criteria
There is a
mypy
CI checking at leastpycroft
andweb
(with--check-untyped-defs
).