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

Add mypy static type checker to CI and necessary fixes to the source code #1717

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

DavidMStraub
Copy link
Member

@DavidMStraub DavidMStraub commented May 4, 2024

This adds mypy static type checks to CI. The PR contains the minimal set of changes to the code base needed to make mypy pass. Type hints are only added where needed by mypy, in most cases class/instance attributes initialized to empty lists or dicts. Some smaller bugs or inconsistencies discovered by mypy are also fixed.

gramps/cli/clidbman.py Outdated Show resolved Hide resolved
gramps/gen/datehandler/_dateparser.py Show resolved Hide resolved
gramps/gen/datehandler/_datestrings.py Outdated Show resolved Hide resolved
gramps/gen/filters/rules/_rule.py Outdated Show resolved Hide resolved
gramps/gen/utils/alive.py Show resolved Hide resolved
gramps/gen/utils/alive.py Show resolved Hide resolved
Comment on lines 58 to 60
from ._reportdialog import ReportDialog

from ._papermenu import PaperFrame
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand some of these new blank lines.

Choose a reason for hiding this comment

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

Possibly added by an automatic formatter? There are other changes that solely alter the code's formatting.

gramps/plugins/gramplet/filter.py Show resolved Hide resolved
gramps/gen/utils/configmanager.py Outdated Show resolved Hide resolved
gramps/plugins/lib/libhtml.py Outdated Show resolved Hide resolved
test/GrampsLogger/GtkHandler_Test.py Outdated Show resolved Hide resolved
@DavidMStraub
Copy link
Member Author

Thanks for your review @QuLogic. This is now ready for merge. Then I can work on adding type hints.

gramps/gui/dialog.py Outdated Show resolved Hide resolved
gramps/gen/db/dummydb.py Outdated Show resolved Hide resolved
@DavidMStraub
Copy link
Member Author

Thanks @QuLogic, I address the latest 2 comments and rebased on master. Would love to see this merged so I can start adding type hints.

@Nick-Hall
Copy link
Member

I'm planning a maintenance release this weekend. There are a few bugs fixed and two new translations added.

After that I'll merge some of the PR s into master.

@QuLogic
Copy link
Contributor

QuLogic commented Sep 13, 2024

This comment about print_po_snippet is not resolved, though it's marked as such in the GitHub interface.

@DavidMStraub
Copy link
Member Author

Right, now it should be resolved.

Copy link
Member

@dsblank dsblank 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! A lot of work here, and a good idea. Brings code base up to modern standards. Looks good to me.

@@ -42,7 +43,7 @@ class GT0(GrampsType):
# A migration utility might instantiate several of these with
# varying blacklist-specs
class GT1(GT0):
_BLACKLIST = BLIST
_BLACKLIST: list[int] | None = BLIST

Choose a reason for hiding this comment

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

Instead of typing on list (and dict, both in general and not just on this particular line), can we instead type on collections.Sequence, collections.MutableSequence, collections.Mapping, and collections.MutableMapping? In my experience it makes things slightly less dependent on internals (one historical example was dict being ordered as an internal implementation detail until... 3.7?). Concrete benefits are also that return values can be marked immutable, reducing the risk of other code attempting to modify data that should be read-only.

@@ -646,7 +647,7 @@ def __init__(self, rlocale):
super().addLabels(loc)

else:
AlphabeticIndex = localAlphabeticIndex
AlphabeticIndex = localAlphabeticIndex # type: ignore

Choose a reason for hiding this comment

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

mypy allows a # type: ignore[$reason-code] syntax as well where $reason-code is the code (readable machine name) of the specific violation you want to ignore. Doing that, in combination with enabling warn_unused_ignores in your favorite mypy configuration file means that you won't end up accidentally ignoring the wrong things as the codebase changes over time.

@DavidMStraub
Copy link
Member Author

@bartfeenstra thanks for yet another review, but to reiterate, the sole purpose of this PR is to add type checks to the CI and add the minimal set of type hints that silence the type checker.

It is NOT a PR that adds consistent, complete, or meaningful type hints to the Gramps code base. So I'm not willing to spend more work on this. I will wait for it to be merged and then start adding type hints in follow-up PRs.

@emyoulation
Copy link
Contributor

emyoulation commented Nov 5, 2024

When the Type Hints start being added, please add a line to that Pull Request/Discussion to maintain a path for people who have a vested interest in the implementation

Here's a related MantisBT issue:
0012073: [Windows AIO Installation] Add the module Lib/typing.py to the Windows AIO version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants