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

Typing #47

Merged
merged 14 commits into from
Aug 13, 2020
Merged

Typing #47

merged 14 commits into from
Aug 13, 2020

Conversation

dlax
Copy link
Member

@dlax dlax commented Jul 29, 2020

This adds type hints to the whole code base.
Many small changes are introduced as before, mostly to help type checking.
Also needed to disable the "nit-picky mode" of sphinx because some references now fail to be found despite being actually defined (I suspect a bug in sphinx, maybe sphinx-doc/sphinx#7493 or sphinx-doc/sphinx#7450).

@dlax dlax force-pushed the typing branch 3 times, most recently from 97fc238 to 977f83c Compare July 30, 2020 07:45
@dlax dlax force-pushed the typing branch 5 times, most recently from 8dc1f15 to cfe39be Compare August 10, 2020 10:20
This will help with type checking in the next commits.
Below, 'self.lines.index(old_line)' assumes that old_line is not None but
Entry's attribute 'raw_line' may be None. The assertion prevents a TypeError.
This is essentially to help with type checking.
This will help for type checking.
Instead of relying on bdb being imported in pdb.
@codecov-commenter
Copy link

codecov-commenter commented Aug 10, 2020

Codecov Report

Merging #47 into master will decrease coverage by 0.35%.
The diff coverage is 98.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #47      +/-   ##
==========================================
- Coverage   99.73%   99.38%   -0.36%     
==========================================
  Files           7        9       +2     
  Lines         746      808      +62     
==========================================
+ Hits          744      803      +59     
- Misses          2        5       +3     
Flag Coverage Δ
#py26 ?
#py36 99.38% <98.85%> (-0.36%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pgtoolkit/_helpers.py 94.11% <87.50%> (-5.89%) ⬇️
pgtoolkit/conf.py 100.00% <100.00%> (ø)
pgtoolkit/errors.py 100.00% <100.00%> (ø)
pgtoolkit/hba.py 100.00% <100.00%> (ø)
pgtoolkit/log/__init__.py 100.00% <100.00%> (ø)
pgtoolkit/log/__main__.py 100.00% <100.00%> (ø)
pgtoolkit/log/parser.py 100.00% <100.00%> (ø)
pgtoolkit/pgpass.py 100.00% <100.00%> (ø)
pgtoolkit/service.py 97.53% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5e70f2d...c8a4ad0. Read the comment docs.

@dlax dlax marked this pull request as ready for review August 10, 2020 14:18
@dlax dlax requested review from bersace and pgiraud August 10, 2020 14:18
If 'other' is anything else than a PassEntry instance, the previous code would
have lead to a TypeError (no attribute as_tuple or sort_key).

When the operation is not implemented for unhandled types, we return
NotImplemented.
There is no reason to return a list here and a tuple is more usual as a
comparison key (e.g. sys.version_info).
pgtoolkit/log/parser.py Outdated Show resolved Hide resolved
We used monkeytype to infer types from running the test suite. Obtained types
were then edited based on code read, removing mock occurrences in particular,
and then iteratively adjusted to make 'mypy --strict' running cleanly. There
are a couple of '# type: ignore', most of them due to mypy's inference failure
or too much dynamic code.

The minimal mypy configuration is added to setup.cfg and the package is
declared "typed" following https://www.python.org/dev/peps/pep-0561/.

Some interesting notes:

* There are many Union; this sometimes makes the code hard to annotate, thus
  there are some '# type: ignore'. When interesting, we introduce type aliases
  (e.g. 'Value' in conf module).

* It is sometimes needed to declare instance attributes at the class level
  (e.g. in 'Configuration'), especially for attributes that are set to 'None'
  in __init__ method and then updated in another method (like the 'path'
  attribute of 'Configuration' or 'HBA').

* We use overloaded definitions for open_or_return() to express the different
  combinations more precisely that with the single declaration involving
  several Union, which are not compatible together. Strangely, this function
  accepts None as fo_or_path argument but raises a ValueError then; this might
  be worth a refactoring, but client code (in pgtoolkit) relies on this at the
  moment.

* Some '# type: ignore' are needed when changing the type of list elements in
  place (e.g. "values[1] = values[1].split(',')"); possibly this is due to
  inference failure by mypy.

* Finally, we need to disable the "nit-picky mode" of sphinx-build because, for
  some reason, some reference now fails to be found despite being actually
  defined. The consequence being that some type hints are not turned into proper
  links by sphinx. This issue may be addressed later.
@dlax dlax merged commit c8a4ad0 into dalibo:master Aug 13, 2020
@dlax dlax deleted the typing branch August 13, 2020 06:50
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.

None yet

3 participants