Skip to content

Enable mypy for ddtrace core, add type hinting for tracer and filters#2163

Merged
Kyle-Verhoog merged 24 commits into
DataDog:masterfrom
Yun-Kim:mypy-core
Mar 16, 2021
Merged

Enable mypy for ddtrace core, add type hinting for tracer and filters#2163
Kyle-Verhoog merged 24 commits into
DataDog:masterfrom
Yun-Kim:mypy-core

Conversation

@Yun-Kim

@Yun-Kim Yun-Kim commented Mar 13, 2021

Copy link
Copy Markdown
Contributor

Description

This enables mypy checking for ddtrace core, with the trace, filter, compat, files being type hinted.
Since not all of ddtrace core has been type hinted, this ignores some mypy errors (will be fixed in upcoming PRs)

Checklist

  • Added to the correct milestone.
  • Tests provided or description of manual testing performed is included in the code or PR.
  • Library documentation is updated.
  • Corp site documentation is updated (link to the PR).

@Yun-Kim Yun-Kim requested a review from a team as a code owner March 13, 2021 00:16
Comment thread ddtrace/internal/_encoding.pyi Outdated
Comment thread ddtrace/internal/_rand.pyi Outdated
Comment thread ddtrace/tracer.py
Comment thread ddtrace/_hooks.py Outdated
"""

_hooks = attr.ib(init=False, factory=lambda: collections.defaultdict(set))
_hooks = attr.ib(init=False, factory=lambda: collections.defaultdict(set)) # type: Dict[str, Set]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can do this instead:

Suggested change
_hooks = attr.ib(init=False, factory=lambda: collections.defaultdict(set)) # type: Dict[str, Set]
_hooks = attr.ib(init=False, factory=lambda: collections.defaultdict(set), type=Dict[str, Set])

And it'd actually be wrong as Dict and defaultdict are different. There's a type for defaultdict:

https://docs.python.org/3/library/typing.html#typing.DefaultDict

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch! 😄

@jd jd added the changelog/no-changelog A changelog entry is not required for this PR. label Mar 15, 2021
Comment thread ddtrace/tracer.py Outdated
Comment thread ddtrace/tracer.py Outdated
Comment thread ddtrace/internal/writer.py
Comment thread ddtrace/tracer.py
Comment thread ddtrace/tracer.py
Comment thread ddtrace/tracer.py
Comment thread ddtrace/filters.py


class TraceFilter(six.with_metaclass(abc.ABCMeta)):
class TraceFilter(six.with_metaclass(abc.ABCMeta)): # type: ignore[misc]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What was the type error here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems like all the ABCs have type errors 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Here's the mypy error: error: Unsupported dynamic base class "six.with_metaclass" [misc] This might be an issue with the ABCMeta rather than with_metaclass

@brettlangdon

Copy link
Copy Markdown
Member

There are a few functions in compat we didn't add type hints to, should we just do all the functions in that file, seems easy/low hanging fruit?

jd
jd previously approved these changes Mar 15, 2021

@brettlangdon brettlangdon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mostly nits at this point, looking good

Comment thread ddtrace/compat.py Outdated


def get_connection_response(conn):
# type: (...) -> Response

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
# type: (...) -> Response
# type: (httplib.HTTPConnection) -> Response

This should be the correct typing here

@Yun-Kim Yun-Kim Mar 15, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since httplib.HTTPConnection is from six.moves.httplib, which itself causes mypy issues relating to importing attributes, it's not trivial to add this specific connection typing into the typedef. Thoughts on leaving the arg type not hinted and instead clearly stating the HTTPConnection type in the docstrings @Kyle-Verhoog @brettlangdon ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm ok leaving it for now

Comment thread ddtrace/tracer.py Outdated
span_type=None, # type: Optional[str]
):
def wrap(self, name=None, service=None, resource=None, span_type=None):
# type: (Optional[str], Optional[str], Optional[str], Optional[str]) -> Callable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The more detailed we can get with Callable the better.

e.g.

Suggested change
# type: (Optional[str], Optional[str], Optional[str], Optional[str]) -> Callable
# type: (Optional[str], Optional[str], Optional[str], Optional[str]) -> Callable[[Callable[[...], typing.Any], Callable[[...], typing.Any]

(I think that is right, since we return the wrap_decorator which takes a function with any arguments and any return type, and returns a function which takes any arguments with any return type.... ?)

Comment thread ddtrace/compat.py Outdated
from typing import AnyStr
from typing import Text

# from ddtrace.internal.writer import Response

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
# from ddtrace.internal.writer import Response

Comment thread ddtrace/tracer.py

@jd jd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just minor things :)

@mergify

mergify Bot commented Mar 16, 2021

Copy link
Copy Markdown
Contributor

@Yun-Kim this pull request is now in conflict 😩

@mergify mergify Bot added the conflict label Mar 16, 2021
@mergify mergify Bot removed the conflict label Mar 16, 2021
Kyle-Verhoog
Kyle-Verhoog previously approved these changes Mar 16, 2021

@brettlangdon brettlangdon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

small nits, otherwise lgtm

Comment thread ddtrace/tracer.py
Comment thread ddtrace/tracer.py
Comment thread ddtrace/tracer.py Outdated
Comment on lines +665 to +666
def wrap(self, name=None, service=None, resource=None, span_type=None):
# type: (Optional[str], Optional[str], Optional[str], Optional[str]) -> Callable[[Callable[..., Any]], Callable[..., Any]] # noqa

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"noqa" because the line is too long? are we better following this pattern we use as well?

Suggested change
def wrap(self, name=None, service=None, resource=None, span_type=None):
# type: (Optional[str], Optional[str], Optional[str], Optional[str]) -> Callable[[Callable[..., Any]], Callable[..., Any]] # noqa
def wrap(
self,
name=None, # type: Optional[str]
service=None, # type: Optional[str]
resource=None, # type: Optional[str]
span_type=None, # type: Optional[str]
):
# type: (...) -> Callable[[Callable[..., Any]], Callable[..., Any]]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Completely didn't occur to me I can use both type hinting styles haha, fixed

jd
jd previously approved these changes Mar 16, 2021
@Yun-Kim Yun-Kim dismissed stale reviews from jd and Kyle-Verhoog via 61aa5b0 March 16, 2021 15:36

@majorgreys majorgreys left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👏🏽 👏🏽 👏🏽 👏🏽 👏🏽

@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #2163 (b18f7d0) into master (c13f094) will increase coverage by 0.00%.
The diff coverage is 95.55%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2163   +/-   ##
=======================================
  Coverage   91.67%   91.67%           
=======================================
  Files         567      567           
  Lines       40472    40488   +16     
=======================================
+ Hits        37101    37117   +16     
  Misses       3371     3371           
Impacted Files Coverage Δ
ddtrace/internal/writer.py 83.19% <ø> (ø)
ddtrace/compat.py 94.38% <93.33%> (+0.19%) ⬆️
ddtrace/tracer.py 89.57% <95.65%> (+0.38%) ⬆️
ddtrace/_hooks.py 94.11% <100.00%> (+0.36%) ⬆️
ddtrace/filters.py 95.83% <100.00%> (ø)
ddtrace/provider.py 87.09% <100.00%> (ø)
ddtrace/sampler.py 95.73% <100.00%> (ø)
ddtrace/ext/ci.py 64.51% <0.00%> (-3.23%) ⬇️
tests/commands/test_runner.py 0.47% <0.00%> (+0.47%) ⬆️
tests/tracer/runtime/test_runtime_id.py 79.16% <0.00%> (+8.33%) ⬆️

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 c13f094...b18f7d0. Read the comment docs.

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

Labels

changelog/no-changelog A changelog entry is not required for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants