Skip to content

Commit d83fde9

Browse files
Kludexemdnetoaabmass
authored andcommitted
Add type hints to SQLite3 (open-telemetry#3057)
* Add type hints to SQLite3 * Apply suggestions from code review * Update instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/package.py * This is a type alias, pylint is dumb --------- Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Aaron Abbott <[email protected]>
1 parent f2f4d22 commit d83fde9

File tree

2 files changed

+19
-7
lines changed
  • instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3

2 files changed

+19
-7
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/__init__.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,35 @@
3939
---
4040
"""
4141

42+
from __future__ import annotations
43+
4244
import sqlite3
4345
from sqlite3 import dbapi2
44-
from typing import Collection
46+
from typing import Any, Collection, TypeVar, Union
4547

4648
from opentelemetry.instrumentation import dbapi
4749
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
4850
from opentelemetry.instrumentation.sqlite3.package import _instruments
4951
from opentelemetry.instrumentation.sqlite3.version import __version__
52+
from opentelemetry.trace import TracerProvider
5053

5154
# No useful attributes of sqlite3 connection object
5255
_CONNECTION_ATTRIBUTES = {}
5356

5457
_DATABASE_SYSTEM = "sqlite"
5558

59+
SQLite3Connection = TypeVar( # pylint: disable=invalid-name
60+
"SQLite3Connection", bound=Union[sqlite3.Connection, None]
61+
)
62+
5663

5764
class SQLite3Instrumentor(BaseInstrumentor):
5865
_TO_WRAP = [sqlite3, dbapi2]
5966

6067
def instrumentation_dependencies(self) -> Collection[str]:
6168
return _instruments
6269

63-
def _instrument(self, **kwargs):
70+
def _instrument(self, **kwargs: Any) -> None:
6471
"""Integrate with SQLite3 Python library.
6572
https://docs.python.org/3/library/sqlite3.html
6673
"""
@@ -77,13 +84,16 @@ def _instrument(self, **kwargs):
7784
tracer_provider=tracer_provider,
7885
)
7986

80-
def _uninstrument(self, **kwargs):
87+
def _uninstrument(self, **kwargs: Any) -> None:
8188
""" "Disable SQLite3 instrumentation"""
8289
for module in self._TO_WRAP:
8390
dbapi.unwrap_connect(module, "connect")
8491

8592
@staticmethod
86-
def instrument_connection(connection, tracer_provider=None):
93+
def instrument_connection(
94+
connection: SQLite3Connection,
95+
tracer_provider: TracerProvider | None = None,
96+
) -> SQLite3Connection:
8797
"""Enable instrumentation in a SQLite connection.
8898
8999
Args:
@@ -105,7 +115,9 @@ def instrument_connection(connection, tracer_provider=None):
105115
)
106116

107117
@staticmethod
108-
def uninstrument_connection(connection):
118+
def uninstrument_connection(
119+
connection: SQLite3Connection,
120+
) -> SQLite3Connection:
109121
"""Disable instrumentation in a SQLite connection.
110122
111123
Args:

Diff for: instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/package.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from __future__ import annotations
1415

15-
16-
_instruments = tuple()
16+
_instruments: tuple[str, ...] = tuple()

0 commit comments

Comments
 (0)