Skip to content

Improve typings some #389

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

Merged
merged 7 commits into from
Jun 10, 2019
Merged

Improve typings some #389

merged 7 commits into from
Jun 10, 2019

Conversation

bluetech
Copy link
Contributor

@bluetech bluetech commented Jun 6, 2019

The first commit fixes #386.

The second commit is a bug fix that mypy found.

The third commit adds a basic example as requested in #386 (comment).

The fourth commit improves the typings a bit by enabling some more strict mypy options and fixing the errors. I imagine it's quite hard to review so I can leave it off if that's preferable.

bluetech added 4 commits June 6, 2019 21:55
sentry_sdk/__init__.py imports * from sentry_sdk.api. Mypy can't
understand the `@public` trick in sentry_sdk.api, so it thinks that
sentry_sdk.api's __all__ is [], so given code like this

    import sentry_sdk
    sentry_sdk.capture_message("Something went wrong!")

mypy complains

Help it by not using the `@public` trick.

Fixes getsentry#386.
…ss>)

The type initial_client.integrations is Dict[str, something] so the
integration name should be used in this check.
Basic usage, mostly taken from the API docs.

Refs getsentry#386.
Enable some more strict mypy options and deal with the fallout.
@bluetech
Copy link
Contributor Author

bluetech commented Jun 6, 2019

I marked the PR as a draft because the fourth commit still has some errors (all in same spirit), and I don't know for sure what is the proper fix:

sentry_sdk/integrations/threading.py:60: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/rq.py:49: error: Item "None" of "Optional[Client]" has no attribute "flush"
sentry_sdk/integrations/rq.py:100: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/excepthook.py:42: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/atexit.py:51: error: Item "None" of "Optional[Client]" has no attribute "close"
sentry_sdk/integrations/tornado.py:114: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/sanic.py:139: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/aws_lambda.py:34: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/aws_lambda.py:50: error: Item "None" of "Optional[Client]" has no attribute "flush"
sentry_sdk/integrations/pyramid.py:131: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/falcon.py:127: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/bottle.py:105: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/django/__init__.py:229: error: Item "None" of "Optional[Client]" has no attribute "options"
sentry_sdk/integrations/flask.py:181: error: Item "None" of "Optional[Client]" has no attribute "options"

These happen because Hub.client has type Optional[Client], but the code assumes it is not None. Possible fixes are:

  1. Change the type of Hub.client to not be optional? Can it be None?
  2. Add assert client is not None before each call site.
  3. Raise an error if client is None.

Once this is decided this can go through CI (I only tested basic py3.7 configuration).

@untitaker
Copy link
Member

Change the type of Hub.client to not be optional? Can it be None?

It can be None, but if there is an integration available there must be a client available.

I "fixed" it by casting the type to Any 😬. I didn't feel like doing something on runtime, like an assertion or typing.cast.

Thanks, this is great work

@untitaker untitaker marked this pull request as ready for review June 7, 2019 09:13
@untitaker
Copy link
Member

untitaker commented Jun 7, 2019

I hope you find the changes acceptable... since the Any cast is somewhere deep in internals it shouldn't matter to the user.

btw, are you interested in joining the community Slack channel for the SDK? #390

@bluetech
Copy link
Contributor Author

bluetech commented Jun 7, 2019

I hope you find the changes acceptable...

Yes, they look good to me.

btw, are you interested in joining the community Slack channel for the SDK?

I don't use Slack personally. If I run into further issues with the typings I'll be sure to return with a PR though :)

@untitaker untitaker merged commit 8d669f6 into getsentry:master Jun 10, 2019
@untitaker
Copy link
Member

Thanks so far!

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.

Some code structures are not conducive to static typing
2 participants