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

interested in adding typing (mypy) support? #198

Closed
asottile-sentry opened this issue Aug 15, 2023 · 4 comments
Closed

interested in adding typing (mypy) support? #198

asottile-sentry opened this issue Aug 15, 2023 · 4 comments

Comments

@asottile-sentry
Copy link
Contributor

this would allow users to typecheck their usage of this middleware -- it seems pretty simple (there's only a few public functions) and could be checked in CI with mypy

for now I'm creating some .pyi stubs in sentry which are enough for my use right now -- but it might be nice to share these more broadly via this package

asottile-sentry added a commit to getsentry/sentry that referenced this issue Aug 15, 2023
upstream seems not super active so I sent an issue there and I'm going
to plop stubs here for now
mozilla/django-csp#198
@stevejalim
Copy link
Contributor

Hi @asottile-sentry - been a while since you opened this, but I'm dusting off the project.

Your suggestion makes sense, yeah. If you'd be up for sharing the .pyis via the project, that sounds cool. main is healthy now and has GH Actions for CI so feel free to PR off that branch if and whenever you fancy it. Cheers!

@Greyvend
Copy link

Greyvend commented May 1, 2024

Any updates on this? mypy checks are failing with

Skipping analyzing "csp.decorators": module is installed, but missing library stubs or py.typed marker  [import-untyped]

@asottile-sentry
Copy link
Contributor Author

I started adding inline annotations but other things were higher priority -- we use this .pyi file with very incomplete types: https://github.com/getsentry/sentry/tree/9a31bb0dc5073c04e892b7a570d33ca609518321/fixtures/stubs-for-mypy/csp

robhudson added a commit that referenced this issue Jul 1, 2024
* Add basepython entries for pypy. This fixes running tox locally.
* Fix the cpython version mapping in [gh-actions]. The github action
  tests for cpython versions are running against the latest Django,
  instead of the set of possible Django versions.
* Add mypy for type checking
* Handle case where config is None
* Use getattr, setattr for dynamic attribute access
  - mypy complains when reading or setting a attribute that is not defined
  on the class, such as HttpRequest.csp_nonce. This updates the code to
  use getattr and setattr to access these dynamically added attributes and
  for Django settings.
* Use tuples where requested
  - Both startswith() and parser.parse_statements take a tuple rather than a list.
* Add type hints
* Refactor ScriptTestBase
  - Althought the code `template.render(context)` looked similar, mypy
  complained that Django's Template could not take a dict. Rather than
  switch on types, refactor `make_context` and `make_template` into
  `render`, which hides the typing details between Django templates and
  extension templates like Jinja2.
* Fix Sphinx doc generation without setuptools
* Add `pip install -e ".[dev]"`
* Update docs for typing, etc.
* Add PEP 561 py.typed file
* Bump Django dependency to 4.2+
* Replace `HttpResponse` type with `HttpResponseBase`
* Update CHANGES file

---------

Co-authored-by: Rob Hudson <[email protected]>
@robhudson
Copy link
Member

Type hints were added in #228. Thank you for the suggestion.

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

No branches or pull requests

4 participants