Skip to content

Commit 7acbfb4

Browse files
Make pyright run in standard mode (#4425)
* first init * found tox.ini updates, and searched for references of mypy. * found tox.ini updates, and searched for references of mypy. * implement suggustions * fix based on feedback, rm and updates to misc_0 ci/cd. * fix commits. * fix commits. * keep mypy and run pyright in standard using strict list Signed-off-by: emdneto <[email protected]> * pyright ignore Signed-off-by: emdneto <[email protected]> * enable reportPrivateUsage Signed-off-by: emdneto <[email protected]> * use type for ignore Signed-off-by: emdneto <[email protected]> * please mypy Signed-off-by: emdneto <[email protected]> --------- Signed-off-by: emdneto <[email protected]> Co-authored-by: emdneto <[email protected]>
1 parent b139a97 commit 7acbfb4

File tree

12 files changed

+45
-33
lines changed

12 files changed

+45
-33
lines changed

.github/workflows/misc_0.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ jobs:
8888
- name: Run tests
8989
run: tox -e mypyinstalled
9090

91-
pyright:
92-
name: pyright
91+
typecheck:
92+
name: typecheck
9393
runs-on: ubuntu-latest
9494
steps:
9595
- name: Checkout repo @ SHA - ${{ github.sha }}
@@ -104,7 +104,7 @@ jobs:
104104
run: pip install tox
105105

106106
- name: Run tests
107-
run: tox -e pyright
107+
run: tox -e typecheck
108108

109109
docs:
110110
name: docs

CONTRIBUTING.md

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ You can run `tox` with the following arguments:
6060
- `tox -e lint-some-package` to run lint checks on `some-package`
6161
- `tox -e generate-workflows` to run creation of new CI workflows if tox environments have been updated
6262
- `tox -e ruff` to run ruff linter and formatter checks against the entire codebase
63+
- `tox -e typecheck` to run pyright against entire code base.
64+
- `tox -e public-symbols-check` to run public_symbols_checker.py.
65+
- `tox -e docker-tests-{otlpexporter,opencensus}` to run tests in both or either one location.
66+
- `tox -e tracecontext` to run integration tests for tracecontext.
6367
- `tox -e precommit` to run all `pre-commit` actions
6468

6569
`ruff check` and `ruff format` are executed when `tox -e ruff` is run. We strongly recommend you to configure [pre-commit](https://pre-commit.com/) locally to run `ruff` automatically before each commit by installing it as git hooks. You just need to [install pre-commit](https://pre-commit.com/#install) in your environment:

dev-requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pylint==3.2.1
22
httpretty==1.1.4
3+
pyright==1.1.396
34
mypy==1.9.0
45
sphinx==7.1.2
56
sphinx-rtd-theme==2.0.0rc4

mypy-requirements.txt

-1
This file was deleted.

opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def create_gauge( # type: ignore # pylint: disable=no-self-use
447447
name: str,
448448
unit: str = "",
449449
description: str = "",
450-
) -> Gauge:
450+
) -> Gauge: # pyright: ignore[reportReturnType]
451451
"""Creates a ``Gauge`` instrument
452452
453453
Args:

opentelemetry-api/src/opentelemetry/util/_decorator.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ def __enter__(self) -> R:
6262
except StopIteration:
6363
raise RuntimeError("generator didn't yield") from None
6464

65-
def __call__(self, func: V) -> V:
65+
def __call__(self, func: V) -> V: # pyright: ignore [reportIncompatibleMethodOverride]
6666
if asyncio.iscoroutinefunction(func):
6767

6868
@functools.wraps(func) # type: ignore
69-
async def async_wrapper(*args: Pargs, **kwargs: Pkwargs) -> R:
69+
async def async_wrapper(*args: Pargs, **kwargs: Pkwargs) -> R: # pyright: ignore [reportInvalidTypeVarUse]
7070
with self._recreate_cm(): # type: ignore
7171
return await func(*args, **kwargs) # type: ignore
7272

@@ -78,8 +78,8 @@ def _agnosticcontextmanager(
7878
func: "Callable[P, Iterator[R]]",
7979
) -> "Callable[P, _AgnosticContextManager[R]]":
8080
@functools.wraps(func)
81-
def helper(*args: Pargs, **kwargs: Pkwargs) -> _AgnosticContextManager[R]:
82-
return _AgnosticContextManager(func, args, kwargs)
81+
def helper(*args: Pargs, **kwargs: Pkwargs) -> _AgnosticContextManager[R]: # pyright: ignore [reportInvalidTypeVarUse]
82+
return _AgnosticContextManager(func, args, kwargs) # pyright: ignore [reportArgumentType]
8383

8484
# Ignoring the type to keep the original signature of the function
8585
return helper # type: ignore[return-value]

opentelemetry-api/tests/trace/test_globals.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ class TestUseSpanException(Exception):
155155
raise TestUseSpanException("test error")
156156

157157
self.assertEqual(
158-
test_span.recorded_status.status_code, StatusCode.ERROR
158+
test_span.recorded_status.status_code, # type: ignore[reportAttributeAccessIssue]
159+
StatusCode.ERROR,
159160
)
160161
self.assertEqual(
161-
test_span.recorded_status.description,
162+
test_span.recorded_status.description, # type: ignore[reportAttributeAccessIssue]
162163
"TestUseSpanException: test error",
163164
)

opentelemetry-api/tests/util/test_once.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def test_once_single_thread(self):
2424
self.assertEqual(once_func.call_count, 0)
2525

2626
# first call should run
27-
called = once.do_once(once_func)
27+
called = once.do_once(once_func) # type: ignore[reportArgumentType]
2828
self.assertTrue(called)
2929
self.assertEqual(once_func.call_count, 1)
3030

3131
# subsequent calls do nothing
32-
called = once.do_once(once_func)
32+
called = once.do_once(once_func) # type: ignore[reportArgumentType]
3333
self.assertFalse(called)
3434
self.assertEqual(once_func.call_count, 1)
3535

@@ -38,7 +38,7 @@ def test_once_many_threads(self):
3838
once = Once()
3939

4040
def run_concurrently() -> bool:
41-
return once.do_once(once_func)
41+
return once.do_once(once_func) # type: ignore[reportArgumentType]
4242

4343
results = self.run_with_many_threads(run_concurrently, num_threads=100)
4444

opentelemetry-sdk/tests/metrics/test_backward_compat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
and PeriodicExportingMetricReader concrete class. Those may freely be modified in a
2323
backward-compatible way for *callers*.
2424
25-
Ideally, we could use mypy for this as well, but SDK is not type checked atm.
25+
Ideally, we could use pyright for this as well, but SDK is not type checked atm.
2626
"""
2727

2828
from typing import Iterable, Sequence

pyproject.toml

+14-9
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ exclude = [
5757
addopts = "-rs -v"
5858
log_cli = true
5959

60-
[tool.pyright]
61-
typeCheckingMode = "off"
62-
reportMissingTypeStubs = "error"
63-
include = [
64-
"opentelemetry-api/src",
65-
"opentelemetry-sdk/src",
66-
"opentelemetry-semantic-conventions/src",
67-
]
68-
6960
[tool.ruff]
7061
# https://docs.astral.sh/ruff/configuration/
7162
target-version = "py38"
@@ -104,3 +95,17 @@ known-third-party = [
10495
"opencensus",
10596
]
10697
known-first-party = ["opentelemetry", "opentelemetry_example_app"]
98+
99+
[tool.pyright]
100+
typeCheckingMode = "standard"
101+
pythonVersion = "3.8"
102+
103+
include = [
104+
"opentelemetry-semantic-conventions",
105+
"opentelemetry-api",
106+
]
107+
108+
# When packages are correct typed add them to the strict list
109+
strict = [
110+
"opentelemetry-semantic-conventions",
111+
]

pyright-requirements.txt

-1
This file was deleted.

tox.ini

+11-8
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ envlist =
8989
spellcheck
9090
tracecontext
9191
mypy,mypyinstalled
92-
pyright
92+
typecheck
9393
docs
9494
docker-tests-{otlpexporter,opencensus}
9595
public-symbols-check
@@ -103,7 +103,8 @@ deps =
103103
coverage: pytest
104104
coverage: pytest-cov
105105

106-
mypy,mypyinstalled: -r {toxinidir}/mypy-requirements.txt
106+
mypy,mypyinstalled: -c {toxinidir}/dev-requirements.txt
107+
mypy,mypyinstalled: mypy
107108

108109
api: -r {toxinidir}/opentelemetry-api/test-requirements.txt
109110

@@ -161,7 +162,6 @@ setenv =
161162
CONTRIB_REPO_SHA={env:CONTRIB_REPO_SHA:main}
162163
CONTRIB_REPO=git+https://github.com/open-telemetry/opentelemetry-python-contrib.git@{env:CONTRIB_REPO_SHA}
163164
mypy: MYPYPATH={toxinidir}/opentelemetry-api/src/:{toxinidir}/opentelemetry-semantic-conventions/src/:{toxinidir}/opentelemetry-sdk/src/:{toxinidir}/tests/opentelemetry-test-utils/src/
164-
165165
commands_pre =
166166
; In order to get a healthy coverage report,
167167
; we have to install packages in editable mode.
@@ -235,11 +235,11 @@ commands =
235235
mypy: mypy --install-types --non-interactive --namespace-packages --explicit-package-bases opentelemetry-sdk/src/opentelemetry/sdk/resources
236236
mypy: mypy --install-types --non-interactive --namespace-packages --explicit-package-bases opentelemetry-semantic-conventions/src/opentelemetry/semconv/
237237

238-
; For test code, we don't want to enforce the full mypy strictness
238+
; For test code, we don't want to enforce the full mypy strictness
239239
mypy: mypy --install-types --non-interactive --namespace-packages --config-file=mypy-relaxed.ini opentelemetry-api/tests/
240240

241-
; Test that mypy can pick up typeinfo from an installed package (otherwise,
242-
; implicit Any due to unfollowed import would result).
241+
; Test that mypy can pick up typeinfo from an installed package (otherwise,
242+
; implicit Any due to unfollowed import would result).
243243
mypyinstalled: mypy --install-types --non-interactive --namespace-packages opentelemetry-api/tests/mypysmoke.py --strict
244244

245245
[testenv:spellcheck]
@@ -347,13 +347,16 @@ commands_pre =
347347
commands =
348348
sh -c "find {toxinidir} -name \*.sh | xargs shellcheck --severity=warning"
349349

350-
[testenv:pyright]
350+
[testenv:typecheck]
351351
basepython: python3
352352
deps =
353-
-r {toxinidir}/pyright-requirements.txt
353+
-c {toxinidir}/dev-requirements.txt
354+
pyright
355+
psutil
354356
-e {toxinidir}/opentelemetry-api
355357
-e {toxinidir}/opentelemetry-semantic-conventions
356358
-e {toxinidir}/opentelemetry-sdk
359+
-e {toxinidir}/tests/opentelemetry-test-utils
357360
commands =
358361
pyright --version
359362
pyright

0 commit comments

Comments
 (0)