How can the type hint be defined to still designate GeoPandas GeoDataFrame as an option but not error when Geopandas is not imported? IE: define a type hint without the module present
Given a class which takes a param typed as a DataFrame or a GeoDataFrame, often only Pandas will be imported, but occasionally GeoPandas will be as well.
The class must be able to take a frame from either interchangeably. If the param is defined as:Union[pandas.Dataframe, geopandas.geodataframe.GeoDataFrame]
an error will occur when GeoPandas has not been imported, and vice versa. Pandas or Geopandas would not be imported solely for the purposes of a type hint.
Any
is an option to define geopandas dataframe, however I hoped to be more concise. Union[pandas.Dataframe, Any]
feels meaningless because it does not supply context as to what type the alternative parameter might be and doesn't cover the case if Geopandas is loaded and not Pandas.
I have reviewed How to type hint with an optional import? but it is not the same situation.