Skip to content

Set up pre-commit #289

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

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

Conversation

ondras12345
Copy link
Contributor

Proposal

pre-commit is a nice tool that can be used to run various linters before each commit is made.
This PR adds a .pre-commit-config.yaml with a basic set of checks and linters. It also fixes typos detected by the codespell check.

Currently, the mypy check fails:

$ pre-commit run --all-files
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check for case conflicts.................................................Passed
check docstring is first.................................................Passed
check that executables have shebangs.................(no files to check)Skipped
check that scripts with shebangs are executable..........................Passed
mixed line ending........................................................Passed
debug statements (python)................................................Passed
detect destroyed symlinks................................................Passed
fix utf-8 byte order marker..............................................Passed
check for merge conflicts................................................Passed
python tests naming......................................................Passed
flake8...................................................................Passed
codespell................................................................Passed
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

OMPython/OMParser.py:37: error: Need type annotation for "result" (hint: "result: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/OMTypedParser.py:112: error: Argument 1 has incompatible type "bool"; expected "str"  [arg-type]
OMPython/OMTypedParser.py:113: error: Argument 1 has incompatible type "bool"; expected "str"  [arg-type]
OMPython/OMTypedParser.py:114: error: Argument 1 has incompatible type "None"; expected "str"  [arg-type]
OMPython/OMCSession.py:92: error: Need type annotation for "_omc_cache" (hint: "_omc_cache: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/OMCSession.py:278: error: Incompatible default for argument "omhome" (default has type "None", argument has type "str")  [assignment]
OMPython/OMCSession.py:278: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
OMPython/OMCSession.py:278: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
OMPython/OMCSession.py:462: error: Incompatible default for argument "omhome" (default has type "None", argument has type "str")  [assignment]
OMPython/OMCSession.py:462: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
OMPython/OMCSession.py:462: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
OMPython/ModelicaSystem.py:44: error: Cannot find implementation or library stub for module named "numpy"  [import-not-found]
OMPython/ModelicaSystem.py:44: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
OMPython/ModelicaSystem.py:366: error: Need type annotation for "quantitiesList" (hint: "quantitiesList: list[<type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:367: error: Need type annotation for "paramlist" (hint: "paramlist: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:368: error: Need type annotation for "inputlist" (hint: "inputlist: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:369: error: Need type annotation for "outputlist" (hint: "outputlist: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:370: error: Need type annotation for "continuouslist" (hint: "continuouslist: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:371: error: Need type annotation for "simulateOptions" (hint: "simulateOptions: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:372: error: Need type annotation for "overridevariables" (hint: "overridevariables: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:373: error: Need type annotation for "simoptionsoverride" (hint: "simoptionsoverride: dict[<type>, <type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:377: error: Need type annotation for "linearinputs" (hint: "linearinputs: list[<type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:378: error: Need type annotation for "linearoutputs" (hint: "linearoutputs: list[<type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:379: error: Need type annotation for "linearstates" (hint: "linearstates: list[<type>] = ...")  [var-annotated]
OMPython/ModelicaSystem.py:386: error: Argument "omhome" to "OMCSessionZMQ" has incompatible type "str | None"; expected "str"  [arg-type]
OMPython/ModelicaSystem.py:389: error: Argument "commandLineOptions" to "setCommandLineOptions" of "ModelicaSystem" has incompatible type "str | None"; expected "str"  [arg-type]
OMPython/ModelicaSystem.py:826: error: Argument "modelname" to "ModelicaSystemCmd" has incompatible type "str | None"; expected "str"  [arg-type]
OMPython/ModelicaSystem.py:830: error: Incompatible types in assignment (expression has type "Path", variable has type "None")  [assignment]
OMPython/ModelicaSystem.py:832: error: Incompatible types in assignment (expression has type "Path", variable has type "None")  [assignment]
OMPython/ModelicaSystem.py:834: error: Incompatible types in assignment (expression has type "Path", variable has type "None")  [assignment]
OMPython/ModelicaSystem.py:836: error: "None" has no attribute "as_posix"  [attr-defined]
OMPython/ModelicaSystem.py:868: error: Incompatible types in assignment (expression has type "Path", variable has type "str")  [assignment]
OMPython/ModelicaSystem.py:870: error: "str" has no attribute "as_posix"  [attr-defined]
OMPython/ModelicaSystem.py:873: error: "None" has no attribute "is_file"  [attr-defined]
OMPython/ModelicaSystem.py:874: error: "None" has no attribute "unlink"  [attr-defined]
OMPython/ModelicaSystem.py:878: error: "None" has no attribute "is_file"  [attr-defined]
Found 31 errors in 4 files (checked 5 source files)

I think @syntron has fixed at least some of these in his fork. I think we should wait for them to be merged before proceeding with this PR.

In the future, we could configure the github actions test workflow to also run linters via pre-commit.

@syntron
Copy link
Contributor

syntron commented May 28, 2025

See status in #279 (comment) - 14 errors left:

  • 3 in OMTypedParser - Argument 1 has incompatible type ...
  • 11 in ModelicaSystem - Need type annotation for ...

I will prepare the needed changes as PR(s)

@syntron
Copy link
Contributor

syntron commented May 28, 2025

I will prepare the needed changes as PR(s)

Please see PR #290, #291, #292 and #294 to reach this point

@ondras12345 ondras12345 mentioned this pull request Jun 10, 2025
@adeas31 adeas31 added this to the 3.8.0 milestone Jun 11, 2025
@ondras12345
Copy link
Contributor Author

Rebased and fixed remaining mypy warnings. All tests in pre-commit run --all-files pass in 1.7 seconds on my PC. Seems reasonable.

@ondras12345 ondras12345 marked this pull request as ready for review June 11, 2025 21:02
This should make workflow runs with linter errors fail faster.
For some reason, I only get this warning when running in github actions.
@ondras12345
Copy link
Contributor Author

Updated to run pre-commit run --all-files in Github actions CI.

@syntron
Copy link
Contributor

syntron commented Jun 14, 2025

@ondras12345 Would you consider to modify this PR such that it comes without the following parts (changes to OMCSession.py) - these clash with PR #295 such that there are big merge conflicts; both points (mypy warning and spelling fix) are included ...

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.

3 participants