Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Introduce mypy (gradually) #265

Merged
merged 2 commits into from
Jun 10, 2021
Merged

Introduce mypy (gradually) #265

merged 2 commits into from
Jun 10, 2021

Conversation

hukkin
Copy link
Contributor

@hukkin hukkin commented Jun 10, 2021

This PR fixes very few type errors, but adds a working configuration. Please see the mypy configuration in pyproject.toml: most of the codebase is still ignored by mypy, but this is a good base to start building on I think.

@codecov
Copy link

codecov bot commented Jun 10, 2021

Codecov Report

Merging #265 (3ee78f5) into master (8e891d8) will decrease coverage by 0.08%.
The diff coverage is 86.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #265      +/-   ##
==========================================
- Coverage   84.21%   84.12%   -0.09%     
==========================================
  Files          21       21              
  Lines        1755     1758       +3     
  Branches      271      271              
==========================================
+ Hits         1478     1479       +1     
- Misses        233      235       +2     
  Partials       44       44              
Impacted Files Coverage Δ
ward/_run.py 85.45% <0.00%> (-0.79%) ⬇️
ward/_utilities.py 83.33% <0.00%> (-2.88%) ⬇️
ward/_collect.py 85.81% <100.00%> (ø)
ward/_debug.py 63.63% <100.00%> (+0.84%) ⬆️
ward/_diff.py 93.67% <100.00%> (ø)
ward/_suite.py 100.00% <100.00%> (ø)
ward/fixtures.py 95.69% <100.00%> (ø)

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 8d4b4b5...3ee78f5. Read the comment docs.

ward/_diff.py Outdated
Comment on lines 96 to 102
if isinstance(output_lines[-1], str):
line_to_rewrite = output_lines[-1]
else:
line_to_rewrite = output_lines[-1].plain
output_lines[-1] = self.rewrite_line(line, line_to_rewrite, prev_marker)
else:
output_lines.append(line[2:])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This I'm not 100% sure about. Maybe line 99 (before changes) should instead append a Text object instead of str?

Copy link
Owner

Choose a reason for hiding this comment

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

I think we should try to avoid Union where possible, so always appending Text (with no args other than the string) instead of str would make sense. They'll be treated the same by Rich anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes tests fail however, so treatment is not 100% the same 😄

Maybe this should be improved upon in a follow-up?

Copy link
Owner

Choose a reason for hiding this comment

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

I'll take a look at the tests and push a fix to this branch (assuming it's a simple fix, which I think it is)

Copy link
Owner

Choose a reason for hiding this comment

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

I've pushed up a change to always use Text and deleted the if isinstance block that was added. Let me know if there's any issues with that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks great, this is also my preferred solution as long as tests pass!

@@ -129,7 +129,7 @@ def teardown(self):
asyncio.get_event_loop().run_until_complete(awaitable)


def fixture(func=None, *, scope: Optional[Union[Scope, str]] = Scope.Test):
def fixture(func=None, *, scope: Union[Scope, str] = Scope.Test):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I'm also a bit unsure. Is there a good reason that None should be allowed as input? If yes, then it should probably be handled before Scope.from_str(scope).

Copy link
Owner

Choose a reason for hiding this comment

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

This shouldn't be Optional -- your change here is correct :)

@@ -54,7 +55,7 @@ def _get_debugger_hook(hookname: str):
modname, dot, funcname = hookname.rpartition(".")
if dot == "":
modname = "builtins"
module = importlib.import_module(modname)
module: Any = importlib.import_module(modname)

Choose a reason for hiding this comment

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

I think this can be types.ModuleType

Copy link
Contributor Author

@hukkin hukkin Jun 10, 2021

Choose a reason for hiding this comment

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

I think line 60 will be a problem then (accessing Pdb attribute of import pdb. That said, maybe a type: ignore on line 60 is better than this. Or equally good/bad, I don't have a preference really.

Choose a reason for hiding this comment

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

Ah, I see. You could possibly create a Protocol to handle this, but in all honesty it's hardly worth the effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep and the Protocol then doesn't make much sense in the else clause where hookname != "pdb.set_trace".

I think this is just dynamically typed code and should be ignored by static type checker.

@hukkin
Copy link
Contributor Author

hukkin commented Jun 10, 2021

From my side, this PR can already be considered ready to merge, with the idea that follow-up PRs remove the module ignoring lines from pyproject.toml and fix any type checker errors in the respective modules.

Another strategy would be to make this a mega PR that fixes everything (a slightly worse strategy if you ask me but will also work 😄 )

@darrenburns
Copy link
Owner

I agree with the approach of doing this gradually. I'll merge this in now, thanks! :)

@darrenburns darrenburns merged commit 796e1e3 into darrenburns:master Jun 10, 2021
@hukkin hukkin deleted the mypy branch June 10, 2021 12:59
mkuyper added a commit to mkuyper/ward that referenced this pull request Aug 19, 2021
* master: (133 commits)
  Prepare 0.63.0b0
  Distribute type data (PEP 561) (darrenburns#283)
  Type check `ward.testing` (darrenburns#282)
  [pre-commit.ci] pre-commit autoupdate (darrenburns#280)
  Add pretty output for all comparison failures (darrenburns#256)
  Update conf.py
  Update pyproject.toml
  Make sure raises raises assertion error when no exception is raised (darrenburns#281)
  [pre-commit.ci] pre-commit autoupdate (darrenburns#275)
  Fix mypy error
  Prepare 0.62.0b0
  Update writing_tests.rst
  Allow subclasses of specified exception class to pass raises assertion (darrenburns#279)
  Type check `ward.expect`, `ward._fixtures` and `ward._terminal` (darrenburns#274)
  Only require `poetry-core` as build system (darrenburns#277)
  Prepare 0.61.1b0
  Allow Click 7 (darrenburns#272)
  Type check `ward._config` (darrenburns#269)
  Correct plural, singular forms darrenburns#244 (darrenburns#258)
  Prepare 0.61.0b0
  Switch from `toml` to `tomli` for TOML v1 compat (darrenburns#267)
  Introduce mypy (gradually) (darrenburns#265)
  Don't update the lockfile with 'make prep' (darrenburns#266)
  Let Poetry automatically update license and py version classifiers (darrenburns#260)
  Prepare 0.60.1b0
  Fix broken command  (darrenburns#257)
  fix typo (darrenburns#255)
  Prepare 0.60.0b0
  Update live output gif for docs
  Add info on `live` output mode to docs
  Print pretty failure messages for `in` and `not in` (darrenburns#242)
  Add `live` progress style and refactor output and progress styles (darrenburns#233)
  only get test source once (darrenburns#249)
  [pre-commit.ci] pre-commit autoupdate (darrenburns#248)
  pygments dependency removed - no longer required
  Update build.yml
  Update README.md
  Prepare 0.59.0b0
  Use Rich for displaying diffs in errors (darrenburns#235)
  Prepare 0.58.0b0
  Several bug fixes (darrenburns#241)
  combine build and pullrequest workflows (darrenburns#236)
  Add Python 3.10 Beta support to CI (darrenburns#230)
  Update CONTRIBUTING.md
  Prepare 0.57.2b0
  Remove comment from build pipeline
  Bump snok/install-poetry from 1.1.1 to 1.1.6 (darrenburns#232)
  add dependabot.yml for gha version bumps (darrenburns#231)
  Add pre-commit (darrenburns#222)
  fix linter errors (darrenburns#217)
  ...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants