Skip to content

Conversation

Yun-Kim
Copy link
Contributor

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

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
"""

_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
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
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


class TraceFilter(six.with_metaclass(abc.ABCMeta)):
class TraceFilter(six.with_metaclass(abc.ABCMeta)): # type: ignore[misc]
Copy link
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
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
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
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
Copy link
Member

@brettlangdon brettlangdon left a comment

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



def get_connection_response(conn):
# type: (...) -> Response
Copy link
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

Copy link
Contributor Author

@Yun-Kim Yun-Kim Mar 15, 2021

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
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

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
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.... ?)

from typing import AnyStr
from typing import Text

# from ddtrace.internal.writer import Response
Copy link
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

Copy link
Contributor

@jd jd left a comment

Choose a reason for hiding this comment

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

just minor things :)

@mergify
Copy link
Contributor

mergify bot commented Mar 16, 2021

@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
Copy link
Member

@brettlangdon brettlangdon left a comment

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 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
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
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
Copy link
Contributor

@majorgreys majorgreys left a comment

Choose a reason for hiding this comment

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

👏🏽 👏🏽 👏🏽 👏🏽 👏🏽

@codecov-io
Copy link

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