Add Typing to Telemetry Monitor Sinks
This is a breakout ticket from #460 regarding this review suggestion
Description
Currently, the telemetry monitor will collect data with a variety of collectors. These collectors then pass the information off to a sink, that will do something data (write to file, store in database, print to stdout, etc.) Because each collector collects a different number of fields, each with potentially different type, the sink interface needed to be highly dynamic. As such its main method (save) was designed to take in *args of any type.
Python actually has a typing utility for allowing users to type hint complex and dynamic types (TypeVarTuples). We should use this feature to fully type our telemetry monitor data sinks.
Justification
Static analysis can help us catch bugs and silly type errors before they ever reach an end user. Us making this change makes our static analysis much more effective.
Implementation Strategy
A rough implementation might look something like this:
import abc
from typing import Generic
from typing_extension import TypeVarTuple, Unpack
Ts = TypeVarTuple("Ts")
class Sink(abc.ABC, Generic[Unpack[Ts]]):
@abc.abstractmethod
def save(self, *args: Unpack[Ts]) -> None:
# ^^^^
# Now positional only, but makes it MUCH easier to
# find bugs ahead of time IMO
...
class FileSink(
Sink[Unpack[Ts]],
Generic[Unpack[Ts]],
):
...
ints_sink = FileSink[int, int]("ints.csv")
many_types_file_sink = FileSink[str, int, float]("many.csv")
# Mypy can now validate that these are safe
ints_sink.save(1, 2)
many_types_file_sink.save("hello", 1, 3.4)
# and correctly find the error here
# "whoops, got switched!" vvvvvvvv
many_types_file_sink.save("hello", 0.5, 123)
Potential issues with this design include the fact that TupleTypeVar was introduced in python 3.11. Because we are directly inheriting from this class, we cannot simply use a future reference. We might need to add typing_extensions as a direct dependency for SmartSim.
Acceptance Criteria
- [ ] The
Sinkinterface is fully typed w/o relying onAny - [ ] Sub classes of
Sinkare also fully typed - [ ] Mypy is run without error
- [ ] SmartSim is able to build and pass the test suite (WITHOUT THE NEED FOR THE OPTIONAL
[mypy]DEPS)