Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Add typing to tracer.py #328

Merged
merged 6 commits into from
Sep 7, 2021
Merged

Add typing to tracer.py #328

merged 6 commits into from
Sep 7, 2021

Conversation

kasium
Copy link
Contributor

@kasium kasium commented Sep 7, 2021

This change was done by using pyannotate and running the test_tracer tests.
When in doubt, the type is now Any.

Part of #319

Signed-off-by: Kai Mueller 15907922+kasium@users.noreply.github.com

@kasium kasium changed the title Add typing to trace.py Add typing to tracer.py Sep 7, 2021
@codecov
Copy link

codecov bot commented Sep 7, 2021

Codecov Report

Merging #328 (b75eac1) into master (c79ef27) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #328      +/-   ##
==========================================
+ Coverage   95.51%   95.52%   +0.01%     
==========================================
  Files          25       25              
  Lines        2030     2035       +5     
  Branches      274      274              
==========================================
+ Hits         1939     1944       +5     
  Misses         57       57              
  Partials       34       34              
Impacted Files Coverage Δ
jaeger_client/tracer.py 99.36% <100.00%> (+0.02%) ⬆️

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 c79ef27...b75eac1. Read the comment docs.

@kasium
Copy link
Contributor Author

kasium commented Sep 7, 2021

Please note, that for most types I checked the tests and the docu, but I'm no expert in your code. Feel free to check and to provide better/correct types.

Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

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

few nits, otherwise looks great

jaeger_client/tracer.py Outdated Show resolved Hide resolved
jaeger_client/tracer.py Outdated Show resolved Hide resolved
jaeger_client/tracer.py Outdated Show resolved Hide resolved
jaeger_client/tracer.py Outdated Show resolved Hide resolved
jaeger_client/tracer.py Outdated Show resolved Hide resolved
jaeger_client/tracer.py Show resolved Hide resolved
kasium and others added 5 commits September 7, 2021 19:45
This change was done by using pyannotate and running the test_tracer tests.
When in doubt, the type is now Any.

Signed-off-by: Kai Mueller <15907922+kasium@users.noreply.github.com>
Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
Signed-off-by: Kai Mueller <15907922+kasium@users.noreply.github.com>
Signed-off-by: Kai Mueller <15907922+kasium@users.noreply.github.com>
Signed-off-by: Kai Mueller <15907922+kasium@users.noreply.github.com>
Signed-off-by: Kai Mueller <15907922+kasium@users.noreply.github.com>
tags.get(ext_tags.SPAN_KIND) == ext_tags.SPAN_KIND_RPC_SERVER
rpc_server = cast(
bool, tags and tags.get(ext_tags.SPAN_KIND) == ext_tags.SPAN_KIND_RPC_SERVER
)
Copy link
Member

Choose a reason for hiding this comment

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

when I run make lint it works fine without this change. How are you testing typing?

Copy link
Contributor Author

@kasium kasium Sep 7, 2021

Choose a reason for hiding this comment

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

Did you also remove "bool(rpc_server)" from the other line. I see then

jaeger_client/tracer.py:223: error: Argument "join" to "_emit_span_metrics" of "Tracer" has incompatible type "Union[Dict[Any, Any], None, Any]"; expected "Optional[bool]"

Copy link
Member

Choose a reason for hiding this comment

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

$ make lint
flake8 jaeger_client crossdock tests
mypy jaeger_client
Success: no issues found in 36 source files
./scripts/check-license.sh

$ gd | cat
diff --git a/jaeger_client/tracer.py b/jaeger_client/tracer.py
index e4e8e00..e553f6e 100644
--- a/jaeger_client/tracer.py
+++ b/jaeger_client/tracer.py
@@ -22,6 +22,8 @@ import random
 import sys
 import time
 import opentracing
+from typing import Optional
+
 from opentracing import Format, UnsupportedFormatException
 from opentracing.ext import tags as ext_tags
 from opentracing.scope_managers import ThreadLocalScopeManager
@@ -282,7 +284,7 @@ class Tracer(opentracing.Tracer):
         self.sampler.close()
         return self.reporter.close()

-    def _emit_span_metrics(self, span, join=False):
+    def _emit_span_metrics(self, span: Span, join: Optional[bool] = False):
         if span.is_sampled():
             self.metrics.spans_started_sampled(1)
         else:

Copy link
Member

Choose a reason for hiding this comment

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

I did pip install mypy

mypy-0.910
mypy_extensions-0.4.3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, of course if you remove the type annotation of the argument, mypy will not complain 😄

Copy link
Member

Choose a reason for hiding this comment

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

I do have the type declared in the function. I also tried with my original suggestion, still successful:

        rpc_server: bool = tags and \
            tags.get(ext_tags.SPAN_KIND) == ext_tags.SPAN_KIND_RPC_SERVER

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one should fail, because a cast is needed. Not sure, why it's successful on your side. On mine it fails as expected.
Maybe a weird mypy cache issue or something else in your setup. Might also be related to the python version which you running.

However, making a cast is fine, so I see no risk here

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but the cast is ugly code. What errors are you getting with rpc_server: bool = ?

I'm running with Python 3.8.2

Copy link
Contributor Author

@kasium kasium Sep 7, 2021

Choose a reason for hiding this comment

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

jaeger_client/tracer.py:182: error: Incompatible types in assignment (expression has type "Union[Dict[Any, Any], None, Any]", variable has type "bool")

Even a clean doesn't help

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, found a way to write it easier

Signed-off-by: Kai Mueller <15907922+kasium@users.noreply.github.com>
@yurishkuro yurishkuro merged commit 44dc207 into jaegertracing:master Sep 7, 2021
@kasium kasium deleted the typing_tracer branch September 8, 2021 05:11
@kasium kasium mentioned this pull request Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants