fastavro / fastavro

Fast Avro for Python
MIT License
644 stars 172 forks source link

Unparameterized use of generic types results in diagnostics #797

Open shoffmeister opened 2 months ago

shoffmeister commented 2 months ago

The current main branch has implementation in https://github.com/fastavro/fastavro/blob/master/fastavro/types.py which uses Python generic types (Dict, List) with insufficient type parameterization.

DictSchema = Dict
Schema = Union[str, List, DictSchema]
NamedSchemas = Dict[str, Dict]

When used with the pyright / pylance type checker, this effectively pollutes fastavro API usage with type warnings, as "Dict" is seen as "Dict[Unknown]".

I am aware of the existence of the "better_typing" branch at https://github.com/fastavro/fastavro/blob/better_typing/fastavro/types.py and do notice considerable effort going into providing comprehensive typing support.

Still, possibly as a temporary stop-gap measure, refactoring the existing implementation on main to use the Any type, for instance like

DictSchema = Dict[Any, Any]
Schema = Union[str, List[Any], DictSchema]
NamedSchemas = Dict[str, Dict[Any, Any]]

could make a lot of code "type-safe" simply by declaring "anything goes, don't know", effectively suppressing the type warnings at the library end. (cf https://docs.python.org/3/library/typing.html#the-any-type)

The Any type was introduced with Python 3.5, so it should be usable.

Happy to provide a PR if that is a way forward?

scottbelden commented 2 months ago

Yeah, I think this makes sense as a stop gap. With how things are I'm not sure when I'll have time to finish up the other branch. If you are able to make the PR it will be quicker to get in.