Skip to content

🚨 Add and check static type hints using mypy#65

Merged
hf-kklein merged 10 commits intodevelopfrom
introduce_static_typing
Oct 18, 2021
Merged

🚨 Add and check static type hints using mypy#65
hf-kklein merged 10 commits intodevelopfrom
introduce_static_typing

Conversation

@hf-kklein
Copy link
Copy Markdown
Contributor

@hf-kklein hf-kklein commented Oct 15, 2021

Lesson learned: Enums are hard to type. But the way I constructed it now both the tests run and there's no need to ignore the enums during type check.

You need to update the branch protection rules to merge this. First remove the linting status check, then re-add both of the linters.

@hf-kklein hf-kklein self-assigned this Oct 15, 2021
@hf-kklein hf-kklein marked this pull request as ready for review October 15, 2021 08:54
@hf-kklein hf-kklein requested review from a team and hf-krechan October 15, 2021 08:54
@hf-kklein hf-kklein changed the title Add and check static type hints using mypy 🚨 Add and check static type hints using mypy Oct 15, 2021
Copy link
Copy Markdown
Collaborator

@hf-krechan hf-krechan left a comment

Choose a reason for hiding this comment

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

Thank you for implementing mypy
I just have one two questions.

from marshmallow_enum import EnumField
from bo4e.com.rufnummer import Rufnummer, RufnummerSchema
from bo4e.com.adresse import Adresse, AdresseSchema
from marshmallow_enum import EnumField # type:ignore
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we have to check the enum topic again. I think they are not well implemented.

Copy link
Copy Markdown
Contributor Author

@hf-kklein hf-kklein Oct 18, 2021

Choose a reason for hiding this comment

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

In this specific line the problem is the marshmallow_enum package itself.

Comment on lines +17 to +23
def _create_empty_referenzen_list() -> List[ExterneReferenz]:
"""
A method with a type hint to please mypy
https://stackoverflow.com/a/61281305/10009545
:return:
"""
return []
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I do not get the connection between this function and the linked SO.
They are talking about Union issue. but here is no Union.
Maybe I am to tired to see it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the thing is that the empty list [] (of type List[Any]) does not have the type expected here. That's why I'm "hiding" the empty list returned in a function that has a type hint that matches mypys expectations. This idea is copied from the "Approach 2 - Use a type-hinted function for empty list assignment]" of the linked SO answer.

Comment on lines -36 to +39
jobtitel: str = fields.Str(missing=None)
abteilung: str = fields.Str(missing=None)
jobtitel = fields.Str(missing=None)
abteilung = fields.Str(missing=None)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What is the reason that the typehint is not needed here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it was in fact wrong here, because we're not in die class Zustaendigkeit itself but in the corresponding schema whose fields are not strings but fields. The previously wrong type hint here is actually one of the errors that mypy found in our code.

Comment on lines +7 to +16
Anrede = Enum(
"Anrede",
{
"HERR": "HERR",
"FRAU": "FRAU",
"EHELEUTE": "EHELEUTE",
"FIRMA": "FIRMA",
"INDIVIDUELL": "INDIVIDUELL",
},
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Oh ah, I see it's nicer. Did your IDE you any other tool gave you a hint?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

at least mypy does now, because the code is not really executed (at least not multiple lines like f.e. dictionary initialization and its usage as argument to Enum())

@hf-kklein hf-kklein merged commit 8c3e0b2 into develop Oct 18, 2021
@hf-kklein hf-kklein deleted the introduce_static_typing branch October 18, 2021 08:10
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