-
Notifications
You must be signed in to change notification settings - Fork 649
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
Add relaxed mypy check for everything. #201
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
; This is mainly intended for unit tests and such. So proably going forward, we | ||
; will disable even more warnings here. | ||
[mypy] | ||
warn_unused_configs = True | ||
disallow_any_unimported = True | ||
; disallow_any_expr = True | ||
; disallow_any_decorated = True | ||
; disallow_any_explicit = True | ||
; disallow_any_generics = True | ||
disallow_subclassing_any = True | ||
; disallow_untyped_calls = True | ||
; disallow_untyped_defs = True | ||
disallow_incomplete_defs = True | ||
check_untyped_defs = True | ||
disallow_untyped_decorators = True | ||
allow_untyped_globals = True | ||
; Due to disabling some other warnings, unused ignores may occur. | ||
; warn_unused_ignores = True | ||
; warn_return_any = True | ||
strict_equality = True | ||
show_error_codes = True | ||
|
||
[mypy-setuptools] | ||
ignore_missing_imports = True |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,10 +73,10 @@ class MultiSpanProcessor(SpanProcessor): | |
def __init__(self): | ||
# use a tuple to avoid race conditions when adding a new span and | ||
# iterating through it on "on_start" and "on_end". | ||
self._span_processors = () | ||
self._span_processors = () # type: typing.Tuple[SpanProcessor, ...] | ||
self._lock = threading.Lock() | ||
|
||
def add_span_processor(self, span_processor: SpanProcessor): | ||
def add_span_processor(self, span_processor: SpanProcessor) -> None: | ||
"""Adds a SpanProcessor to the list handled by this instance.""" | ||
with self._lock: | ||
self._span_processors = self._span_processors + (span_processor,) | ||
|
@@ -122,11 +122,11 @@ class Span(trace_api.Span): | |
def __init__( | ||
self, | ||
name: str, | ||
context: "trace_api.SpanContext", | ||
context: trace_api.SpanContext, | ||
parent: trace_api.ParentSpan = None, | ||
sampler=None, # TODO | ||
trace_config=None, # TODO | ||
resource=None, # TODO | ||
sampler: None = None, # TODO | ||
trace_config: None = None, # TODO | ||
resource: None = None, # TODO | ||
attributes: types.Attributes = None, # TODO | ||
events: typing.Sequence[trace_api.Event] = None, # TODO | ||
links: typing.Sequence[trace_api.Link] = None, # TODO | ||
|
@@ -140,9 +140,6 @@ def __init__( | |
self.sampler = sampler | ||
self.trace_config = trace_config | ||
self.resource = resource | ||
self.attributes = attributes | ||
self.events = events | ||
self.links = links | ||
self.kind = kind | ||
|
||
self.span_processor = span_processor | ||
|
@@ -165,8 +162,8 @@ def __init__( | |
else: | ||
self.links = BoundedList.from_seq(MAX_NUM_LINKS, links) | ||
|
||
self.end_time = None | ||
self.start_time = None | ||
self.end_time = None # type: typing.Optional[int] | ||
self.start_time = None # type: typing.Optional[int] | ||
|
||
def __repr__(self): | ||
return '{}(name="{}", context={})'.format( | ||
|
@@ -203,9 +200,13 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: | |
def add_event( | ||
self, name: str, attributes: types.Attributes = None | ||
) -> None: | ||
if attributes is None: | ||
attributes = Span.empty_attributes | ||
self.add_lazy_event(trace_api.Event(name, util.time_ns(), attributes)) | ||
self.add_lazy_event( | ||
trace_api.Event( | ||
name, | ||
util.time_ns(), | ||
Span.empty_attributes if attributes is None else attributes, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
) | ||
) | ||
|
||
def add_lazy_event(self, event: trace_api.Event) -> None: | ||
with self._lock: | ||
|
@@ -226,7 +227,9 @@ def add_link( | |
attributes: types.Attributes = None, | ||
) -> None: | ||
if attributes is None: | ||
attributes = Span.empty_attributes | ||
attributes = ( | ||
Span.empty_attributes | ||
) # TODO: empty_attributes is not a Dict. Use Mapping? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds like the right solution to me, why not do it in this PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR is currently marked WIP (maybe I should've made it a Draft PR). Since there are so many "problems" I'm not sure if this is something that is really worthwhile. |
||
self.add_lazy_link(trace_api.Link(link_target_context, attributes)) | ||
|
||
def add_lazy_link(self, link: "trace_api.Link") -> None: | ||
|
@@ -242,7 +245,7 @@ def add_lazy_link(self, link: "trace_api.Link") -> None: | |
return | ||
self.links.append(link) | ||
|
||
def start(self, start_time: int = None): | ||
def start(self, start_time: typing.Optional[int] = None) -> None: | ||
with self._lock: | ||
if not self.is_recording_events(): | ||
return | ||
|
@@ -256,7 +259,7 @@ def start(self, start_time: int = None): | |
return | ||
self.span_processor.on_start(self) | ||
|
||
def end(self, end_time: int = None): | ||
def end(self, end_time: int = None) -> None: | ||
with self._lock: | ||
if not self.is_recording_events(): | ||
return | ||
|
@@ -285,7 +288,7 @@ def is_recording_events(self) -> bool: | |
return True | ||
|
||
|
||
def generate_span_id(): | ||
def generate_span_id() -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These don't seem to change the results of I'm more than happy for internal methods like this not to have type annotations. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you happier without type annotations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are admittedly pretty inoffensive, I just don't think we should require annotations unless they're useful for external users, or we use them to type check our own use of the API. Whether you think this change is net good or net bad probably depends on whether you think it's a bug or a feature that python isn't statically typed. |
||
"""Get a new random span ID. | ||
|
||
Returns: | ||
|
@@ -294,7 +297,7 @@ def generate_span_id(): | |
return random.getrandbits(64) | ||
|
||
|
||
def generate_trace_id(): | ||
def generate_trace_id() -> int: | ||
"""Get a new random trace ID. | ||
|
||
Returns: | ||
|
@@ -327,7 +330,7 @@ def start_span( | |
name: str, | ||
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN, | ||
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, | ||
) -> typing.Iterator["Span"]: | ||
) -> typing.Iterator[trace_api.Span]: | ||
"""See `opentelemetry.trace.Tracer.start_span`.""" | ||
|
||
span = self.create_span(name, parent, kind) | ||
|
@@ -370,8 +373,8 @@ def create_span( | |
|
||
@contextmanager | ||
def use_span( | ||
self, span: Span, end_on_exit: bool = False | ||
) -> typing.Iterator[Span]: | ||
self, span: trace_api.Span, end_on_exit: bool = False | ||
) -> typing.Iterator[trace_api.Span]: | ||
"""See `opentelemetry.trace.Tracer.use_span`.""" | ||
try: | ||
span_snapshot = self._current_span_slot.get() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ class BoundedList(Sequence): | |
|
||
def __init__(self, maxlen): | ||
self.dropped = 0 | ||
self._dq = deque(maxlen=maxlen) | ||
self._dq = deque(maxlen=maxlen) # type: deque | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty surprising that you need these, but I see that you do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the reason is that I should really be using something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense, and does seem like overkill at the moment to me. |
||
self._lock = threading.Lock() | ||
|
||
def __repr__(self): | ||
|
@@ -98,8 +98,8 @@ def __init__(self, maxlen): | |
raise ValueError | ||
self.maxlen = maxlen | ||
self.dropped = 0 | ||
self._dict = OrderedDict() | ||
self._lock = threading.Lock() | ||
self._dict = OrderedDict() # type: OrderedDict | ||
self._lock = threading.Lock() # type: threading.Lock | ||
|
||
def __repr__(self): | ||
return "{}({}, maxlen={})".format( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,20 @@ envlist = | |
py3{4,5,6,7,8}-test-{api,sdk,example-app,ext-wsgi,ext-http-requests} | ||
pypy3-test-{api,sdk,example-app,ext-wsgi,ext-http-requests} | ||
lint | ||
py37-{mypy,mypyinstalled} | ||
py37-{mypy-extra,mypyinstalled} | ||
docs | ||
|
||
[travis] | ||
python = | ||
3.7: py37, lint, docs | ||
3.8: py38 ; Run mypy-extra here because 3.8 is allowed to fail | ||
|
||
[testenv] | ||
deps = | ||
mypy,mypyinstalled: mypy~=0.711 | ||
|
||
setenv = | ||
mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/ | ||
mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/{:}{toxinidir}/opentelemetry-sdk/src/ | ||
|
||
changedir = | ||
test-api: opentelemetry-api/tests | ||
|
@@ -49,6 +50,14 @@ commands = | |
mypy: mypy --namespace-packages opentelemetry-api/src/opentelemetry/ | ||
; For test code, we don't want to enforce the full mypy strictness | ||
mypy: mypy --namespace-packages --config-file=mypy-relaxed.ini opentelemetry-api/tests/ | ||
; For the SDK, we don't require type annotations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it problematic to turn mypy on for the whole directory? |
||
mypy-extra: -mypy --namespace-packages --config-file=mypy-minimal.ini opentelemetry-sdk/src/opentelemetry | ||
mypy-extra: -mypy --namespace-packages --config-file=mypy-minimal.ini opentelemetry-sdk/tests/ | ||
; Same for extensions | ||
mypy-extra: -mypy --namespace-packages --config-file=mypy-minimal.ini ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/ | ||
; mypy-extra: -mypy --namespace-packages --config-file=mypy-minimal.ini ext/opentelemetry-ext-wsgi/tests/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why leave these in but commented? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WIP -- I might comment them back in later. If not, I'll of course add a comment explaining why they are disabled. |
||
mypy-extra: -mypy --namespace-packages --config-file=mypy-minimal.ini ext/opentelemetry-ext-http-requests/src/opentelemetry/ext | ||
; mypy-extra: -mypy --namespace-packages --config-file=mypy-minimal.ini ext/opentelemetry-ext-http-requests/tests/ | ||
|
||
; Test that mypy can pick up typeinfo from an installed package (otherwise, | ||
; implicit Any due to unfollowed import would result). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the behavior of the function, it'll throw now if
items
is null. The type declaration doesn't help since it's called above (inextract
) with an unannotated arg.