Skip to content

Commit

Permalink
use cast to satisfy mypy instead of ignoring type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
helylle committed Dec 9, 2024
1 parent 5092aa2 commit f3c252a
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/eduid/userdb/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from collections.abc import Mapping
from datetime import datetime
from enum import Enum
from typing import Any, Generic, NewType, TypeVar
from typing import Any, Generic, NewType, TypeVar, cast

from pydantic import BaseModel, ConfigDict, Field, field_validator

Expand Down Expand Up @@ -399,10 +399,7 @@ def verified(self) -> list[ListElement]:
Get all the verified elements in the ElementList.
"""
verified_elements = [e for e in self.elements if isinstance(e, VerifiedElement) and e.is_verified]
# mypy figures out the real type of `verified_elements' since isinstance() is used above and complains
# error: Incompatible return value type (got "List[VerifiedElement]", expected "List[ListElement]")
return verified_elements # type: ignore[return-value]
return cast(list[ListElement], [e for e in self.elements if isinstance(e, VerifiedElement) and e.is_verified])


class PrimaryElementList(VerifiedElementList[ListElement], Generic[ListElement], ABC):
Expand Down Expand Up @@ -450,9 +447,7 @@ def primary(self) -> ListElement | None:
if not isinstance(match, PrimaryElement):
raise UserDBValueError(f"Primary element {repr(match)} is not of type PrimaryElement")

# mypy figures out the real type of match since isinstance() is used above and complains
# error: Incompatible return value type (got "PrimaryElement", expected "Optional[ListElement]")
return match # type: ignore[return-value]
return cast(ListElement, match)

def set_primary(self, key: ElementKey) -> None:
"""
Expand Down Expand Up @@ -483,10 +478,7 @@ def set_primary(self, key: ElementKey) -> None:
raise UserDBValueError(f"Element {repr(this)} is not of type PrimaryElement")
this.is_primary = bool(this.key == key)
new += [this]
# mypy figures out the real type of `new' since isinstance() is used above and complains
# error: Incompatible types in assignment (expression has type "List[PrimaryElement]",
# variable has type "List[ListElement]")
self.elements = new # type: ignore[assignment]
self.elements = cast(list[ListElement], new)

@classmethod
def _get_primary(cls, elements: list[ListElement]) -> ListElement | None:
Expand All @@ -512,9 +504,7 @@ def _get_primary(cls, elements: list[ListElement]) -> ListElement | None:
if not primary.is_verified:
raise PrimaryElementViolation("Primary element is not verified")

# mypy figures out the real type of `res[0]' since isinstance() is used above and complains
# error: Incompatible return value type (got "PrimaryElement", expected "ListElement | None")
return res[0] # type: ignore[return-value]
return cast(ListElement, res[0])

def remove(self, key: ElementKey) -> None:
"""
Expand Down

0 comments on commit f3c252a

Please sign in to comment.