Skip to content

Fix mypy errors in models.py and complete type hints

Summary

  • mypy reports several errors for models.py, mostly related to variables that can potentially be None. This should be checked and potentially fixed (see Job #277216 failed for b8b2047c, or error list below).
  • In addition, missing type hints should be added.

Expected Behavior

mypy should not report any errors for models.py.

Steps to Reproduce the Problem

Please describe, step by step, how others can reproduce the problem. Please try these steps for yourself on a clean system.

  1. run make mypy or look at failing piplines: Job #277216 failed for b8b2047c:

Specifications

  • Version: current dev (0.7.X)
  • Platform: Fedora 39

Possible fixes

Investigate whether the mypy errors are real and apply fixes (e.g. handle None properly).

Job #277216 failed for b8b2047c:

mypy errors
src/linkahead/common/models.py:390: error: Item "None" of "Optional[ACL]" has no attribute "grant"  [union-attr]
src/linkahead/common/models.py:432: error: Item "None" of "Optional[ACL]" has no attribute "deny"  [union-attr]
src/linkahead/common/models.py:458: error: Unsupported right operand type for in ("Optional[Permissions]")  [operator]
src/linkahead/common/models.py:460: error: Item "None" of "Optional[ACL]" has no attribute "is_permitted"  [union-attr]
src/linkahead/common/models.py:460: error: Missing positional argument "role" in call to "is_permitted"  [call-arg]
src/linkahead/common/models.py:539: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:542: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:543: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:544: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:545: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:546: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:548: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:549: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:713: error: Argument "datatype" to "Property" has incompatible type "Optional[str]"; expected "Optional[Literal['DOUBLE', 'REFERENCE', 'TEXT', 'DATETIME', 'INTEGER', 'FILE', 'BOOLEAN']]"  [arg-type]
src/linkahead/common/models.py:809: error: Argument "inheritance" to "Parent" has incompatible type "Union[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE'], str, None]"; expected "Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']]"  [arg-type]
src/linkahead/common/models.py:1049: error: Incompatible types in assignment (expression has type "Union[list[str], tuple[()]]", variable has type "Union[str, list[str], tuple[str]]")  [assignment]
src/linkahead/common/models.py:1083: error: Item "None" of "Optional[Property]" has no attribute "value"  [union-attr]
src/linkahead/common/models.py:1198: error: Incompatible default for argument "add_properties" (default has type "str", argument has type "Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']]")  [assignment]
src/linkahead/common/models.py:1304: error: Argument 2 to "to_xml" of "_Properties" has incompatible type "Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']]"; expected "Union[str, Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']]"  [arg-type]
src/linkahead/common/models.py:1852: error: Signature of "to_xml" incompatible with supertype "Entity"  [override]
src/linkahead/common/models.py:1852: note:      Superclass:
src/linkahead/common/models.py:1852: note:          def to_xml(self, xml: Optional[_Element] = ..., add_properties: Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']] = ..., local_serialization: bool = ...) -> _Element
src/linkahead/common/models.py:1852: note:      Subclass:
src/linkahead/common/models.py:1852: note:          def to_xml(self, xml: Optional[_Element] = ..., add_properties: Any = ...) -> Any
src/linkahead/common/models.py:1922: error: Signature of "to_xml" incompatible with supertype "Entity"  [override]
src/linkahead/common/models.py:1922: note:      Superclass:
src/linkahead/common/models.py:1922: note:          def to_xml(self, xml: Optional[_Element] = ..., add_properties: Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']] = ..., local_serialization: bool = ...) -> _Element
src/linkahead/common/models.py:1922: note:      Subclass:
src/linkahead/common/models.py:1922: note:          def to_xml(self, xml: Optional[_Element] = ..., add_properties: Any = ...) -> Any
src/linkahead/common/models.py:2075: error: Signature of "to_xml" incompatible with supertype "Entity"  [override]
src/linkahead/common/models.py:2075: note:      Superclass:
src/linkahead/common/models.py:2075: note:          def to_xml(self, xml: Optional[_Element] = ..., add_properties: Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']] = ..., local_serialization: bool = ...) -> _Element
src/linkahead/common/models.py:2075: note:      Subclass:
src/linkahead/common/models.py:2075: note:          def to_xml(self, xml: Optional[_Element] = ..., add_properties: Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']] = ...) -> _Element
src/linkahead/common/models.py:2078: error: Incompatible default for argument "add_properties" (default has type "str", argument has type "Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']]")  [assignment]
src/linkahead/common/models.py:2178: error: Incompatible default for argument "add_properties" (default has type "str", argument has type "Optional[Literal['OBLIGATORY', 'SUGGESTED', 'RECOMMENDED', 'FIX', 'ALL', 'NONE']]")  [assignment]
src/linkahead/common/models.py:3404: error: Invalid index type "Container" for "dict[Entity, Optional[list[Entity]]]"; expected type "Entity"  [index]
src/linkahead/common/models.py:4155: error: Argument 2 to "set" of "_Element" has incompatible type "Optional[str]"; expected "Union[str, bytes, QName]"  [arg-type]
src/linkahead/common/models.py:4160: error: Argument 2 to "set" of "_Element" has incompatible type "Optional[str]"; expected "Union[str, bytes, QName]"  [arg-type]
src/linkahead/common/models.py:4217: error: Argument "priority" to "grant" of "ACL" has incompatible type "Optional[str]"; expected "bool"  [arg-type]
src/linkahead/common/models.py:4221: error: Argument "priority" to "deny" of "ACL" has incompatible type "Optional[str]"; expected "bool"  [arg-type]
src/linkahead/common/models.py:4246: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
src/linkahead/common/models.py:4247: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
src/linkahead/common/models.py:4248: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
src/linkahead/common/models.py:4249: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
src/linkahead/common/models.py:4567: error: Argument 1 to "int" has incompatible type "Optional[str]"; expected "Union[str, Buffer, SupportsInt, SupportsIndex, SupportsTrunc]"  [arg-type]
src/linkahead/common/models.py:4572: error: Item "None" of "Optional[str]" has no attribute "lower"  [union-attr]
src/linkahead/common/models.py:4659: error: Item "None" of "Optional[str]" has no attribute "split"  [union-attr]
src/linkahead/common/models.py:4886: error: Argument "name" to "Permission" has incompatible type "Optional[str]"; expected "str"  [arg-type]
src/linkahead/common/models.py:4929: error: "None" not callable  [misc]
src/linkahead/common/models.py:4957: error: Item "None" of "Optional[_Element]" has no attribute "attrib"  [union-attr]
src/linkahead/common/models.py:4957: error: Argument "body" to "Message" has incompatible type "Union[_Attrib, Any]"; expected "Optional[str]"  [arg-type]
src/linkahead/common/models.py:4966: error: Item "None" of "Optional[str]" has no attribute "strip"  [union-attr]
src/linkahead/common/models.py:4968: error: Argument "code" to "Message" has incompatible type "Optional[str]"; expected "Optional[int]"  [arg-type]
src/linkahead/common/models.py:5006: error: Incompatible types in assignment (expression has type "EntityHasNoDatatypeError", variable has type "EntityDoesNotExistError")  [assignment]
src/linkahead/common/models.py:5009: error: Incompatible types in assignment (expression has type "AuthorizationError", variable has type "EntityDoesNotExistError")  [assignment]
src/linkahead/common/models.py:5012: error: Incompatible types in assignment (expression has type "UniqueNamesError", variable has type "EntityDoesNotExistError")  [assignment]
src/linkahead/common/models.py:5015: error: Incompatible types in assignment (expression has type "UnqualifiedPropertiesError", variable has type "EntityDoesNotExistError")  [assignment]
src/linkahead/common/models.py:5023: error: Incompatible types in assignment (expression has type "UnqualifiedParentsError", variable has type "EntityDoesNotExistError")  [assignment]
src/linkahead/common/models.py:5030: error: Incompatible types in assignment (expression has type "EntityError", variable has type "EntityDoesNotExistError")  [assignment]
src/linkahead/common/models.py:5032: error: Incompatible types in assignment (expression has type "EntityError", variable has type "EntityDoesNotExistError")  [assignment]
src/linkahead/common/models.py:5089: error: Argument 2 to "_evaluate_and_add_error" has incompatible type "Union[Entity, QueryTemplate, Container]"; expected "Union[Entity, Container]"  [arg-type]
Edited by Joscha Schmiedt
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information