Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc typing fixes in not annotated functions #6389

Merged
merged 2 commits into from
Jun 26, 2024
Merged

Misc typing fixes in not annotated functions #6389

merged 2 commits into from
Jun 26, 2024

Conversation

marcospri
Copy link
Member

@marcospri marcospri commented Jun 25, 2024

Quick fixes with the goal of enabling mypy's check_untyped_defs

First round of fixes.

@@ -39,7 +39,7 @@ class _Setting:
"""The properties of a setting and how to read it."""

name: str
read_from: str | None = None
read_from: str = None # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use this pattern, allowing None in a dataclass field to provide a default in __post_init__

Ignore the type here to avoid multiple issues about it being nullable all over the place.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably just remove the read_from feature, it's only used once

lms/extensions/feature_flags/__init__.py Outdated Show resolved Hide resolved
@@ -92,7 +92,7 @@ class ApplicationInstance(CreatedUpdatedMixin, Base):

consumer_key = sa.Column(sa.Unicode, unique=True, nullable=True)
shared_secret = sa.Column(sa.Unicode, nullable=False)
lms_url = sa.Column(sa.Unicode(2048), nullable=False)
lms_url: Mapped[str] = mapped_column(sa.Unicode(2048), nullable=False)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably automate a conversion to this style for all models. Doing only the ones that solve issues in this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think nullable=False is the default when using Mapped[str]. (If you want it to be nullable you have to do Mapped[str | None])

from lms.services.exceptions import CanvasAPIPermissionError, FileNotFoundInCourse


class CanvasService:
"""A high level Canvas service."""

api = None
api: CanvasAPIClient = None # type:ignore
Copy link
Member Author

@marcospri marcospri Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT this is only here for the tests benefict. Ignore this declaration so all the code below assumes the right type.

@@ -43,7 +43,7 @@ def get_by_id(self, id_: int) -> Organization | None:

return self._organization_search_query(id_=id_).one_or_none()

def get_by_public_id(self, public_id: str) -> list | None:
def get_by_public_id(self, public_id: str) -> Organization | None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was wrong

@marcospri marcospri requested a review from seanh June 25, 2024 08:47
Copy link
Collaborator

@seanh seanh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a quick test of dumping the DB schema from a DB created on main and from one created on this branch diffing them, the only difference is the change in the type of the deployment_id column which I noted. Probably a good idea to do this test whenever changing the models

@@ -39,7 +39,7 @@ class _Setting:
"""The properties of a setting and how to read it."""

name: str
read_from: str | None = None
read_from: str = None # type: ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably just remove the read_from feature, it's only used once

"""Return a SA column type to store the python enum.Enum as a varchar in a table."""
return sa.Column(
return mapped_column(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is probably a pretty good example of SQLAlchemy cleverness that we'd be better off without. If we want to add Python-only validation to a column, SQLAlchemy has a simple way to do that: https://docs.sqlalchemy.org/en/20/orm/mapped_attributes.html#simple-validators

@@ -92,7 +92,7 @@ class ApplicationInstance(CreatedUpdatedMixin, Base):

consumer_key = sa.Column(sa.Unicode, unique=True, nullable=True)
shared_secret = sa.Column(sa.Unicode, nullable=False)
lms_url = sa.Column(sa.Unicode(2048), nullable=False)
lms_url: Mapped[str] = mapped_column(sa.Unicode(2048), nullable=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think nullable=False is the default when using Mapped[str]. (If you want it to be nullable you have to do Mapped[str | None])

@@ -39,27 +40,25 @@ class LTIRoleOverride(Base):

id = sa.Column(sa.Integer(), autoincrement=True, primary_key=True)

lti_role_id = sa.Column(
sa.Integer(),
lti_role_id: Mapped[int] = mapped_column(
sa.ForeignKey("lti_role.id", ondelete="cascade"),
nullable=True,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this has nullable=True shouldn't it be Mapped[int | None]? (Then you wouldn't need the nullable=True)


application_instance_id = sa.Column(
sa.Integer(),
application_instance_id: Mapped[int] = mapped_column(
sa.ForeignKey("application_instances.id", ondelete="cascade"),
nullable=False,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think nullable=False is the default with Mapped

)

lti_registration = sa.orm.relationship(
"LTIRegistration", back_populates="application_instances"
)

# Unique identifier of this instance per LTIRegistration
deployment_id = sa.Column(sa.UnicodeText, nullable=True)
deployment_id: Mapped[str | None] = mapped_column()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the type of the column from text to character varying

@marcospri marcospri merged commit f9c8246 into main Jun 26, 2024
9 checks passed
@marcospri marcospri deleted the mypy-functions branch June 26, 2024 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants