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

Type annotate code via mypy (strict) typechecking all flake8-bugbear code #478

Open
cooperlees opened this issue Jun 24, 2024 · 4 comments

Comments

@cooperlees
Copy link
Collaborator

We should add type hinting everywhere across the code.

Happy for gradual, but eventually working up to mypy strict typing.

@jakkdl
Copy link
Contributor

jakkdl commented Jun 25, 2024

There's a couple nasty things that need restructuring before we can add a loose mypy run, in particular the magic with namedtuple+partial for Error which means that it can't be used as a type and any assignments to e.g. B005.methods is a no-go.

I'd suggest doing something similar to how we do it in flake8-async, https://github.com/python-trio/flake8-async/blob/c977a2e8d471d474ef13ca9b746acdd63600ea52/flake8_async/base.py#L62
With making Error a class with an abstract message attribute, and then define all the error codes as subclasses.
For yielding the tuples to flake8 we can then implement __iter__.

I love having types when developing, so whenever I contribute I don't mind adding types for whatever my contribution touches.

@cooperlees
Copy link
Collaborator Author

Thanks for calling this out. I expected this. I've never had a code base where adding typing has not unlocked some evil evil code. That's it's main advantage, if typing is hard, probably not the cleanest code.

Many thanks for the example. I'll try copy it if I get to this. I'm due for some actual code changes here. Thanks for adding typing as you add new features + fix bugs.

Goal 1: Try get mypy running on minimum typed code and iteratively work from there.

@JelleZijlstra
Copy link
Collaborator

I added basic mypy checking in #482. I converted all the BNNN.abcd assignments to separate globals and annotated a bunch of instance attributes; otherwise it was mostly smooth sailing.

@JelleZijlstra
Copy link
Collaborator

I added a few more types in #483. Here's a few next steps to take:

  • Annotate more of the code
  • Turn on mypy strict mode (e.g. --check-untyped-defs etc.)
  • Run type checking on multiple Python versions (currently just on 3.12) to catch version-specific mistakes

I might do a few of those things at some point but no guarantees. Others should feel free to put up incremental PRs too.

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

No branches or pull requests

3 participants