You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't like typing annotation in the code, it's ugly and makes the code difficult to read.
However, numpy has stub files .pyi for all modules. Those would be appropriate also for statsmodels.
I tried to create stub files with mypy's stubgen, but those are using Any for all parameters.
I started with statsmodels.stats because that has relatively simple functions.
The model modules will need classes or class hierarchies as types.
Can start literally anywhere. statsmodels will be pretty hard to types in general since many parts poke attributes onto classes. But simple functions are a good place to start.
My guess the problem for using generic typing generators is that we are using mainly numpy and pandas data structures.
AFAICS, those tools will not match a type array_like.
statsmodels.stats would have many standalone function, but the trend is to return a Holder instance that dynamically generates case specific attributes with
The biggest challenge with statsmodels is there are too many attributes that are totally missing when not used. These should all be replaced with code like self.attr = None and then if something: self.attr = value. Statsmodels has a lot of code that uses hasattr and getattr(...,... default). Both of these are anti pattern imo.
there are too many attributes that are totally missing when not used.
the bad reason
we still have a lot of inconsistencies across models. This is mainly because models are and have been added and developed without having strict requirements beyond the basics.
the positive reasons
missing attributes and methods for generic supporting code as in post-estimation. Those are not yet available if attributes and methods have not yet been added to the model. (this is my main more recent case for adding hasattr and getattr.)
avoid cluttering of class and instances namespace with attributes that are not applicable.
in several cases I wasn't sure whether to add a None attribute or just leave it out. hasattr and getattr(...,... default) look like pretty standard Python to me.
When writing new models/results we don't need to know which other attributes and methods we will eventually need or will never apply..
Activity
josef-pkt commentedon Feb 20, 2023
#8152 PR that add some typing support to (mainly) tsa
luxedo commentedon Aug 7, 2024
Any updates on this? I can help typing
bashtage commentedon Aug 12, 2024
Can start literally anywhere. statsmodels will be pretty hard to types in general since many parts poke attributes onto classes. But simple functions are a good place to start.
yangdanny97 commentedon Nov 13, 2024
If you want to bootstrap the codebase with types, you could try using autotyping or Monkeytype
https://github.com/JelleZijlstra/autotyping
https://github.com/Instagram/MonkeyType
To make sure the types are correct it would be good to have either mypy or pyright running as a github action.
josef-pkt commentedon Nov 13, 2024
My guess the problem for using generic typing generators is that we are using mainly numpy and pandas data structures.
AFAICS, those tools will not match a type
array_like.statsmodels.stats would have many standalone function, but the trend is to return a Holder instance that dynamically generates case specific attributes with
some,especially older, functions are easier because they return for example
float or ndarraybrief look without finding answers
Does our inheritance structure in models and results classes create problems for mypy.
(I don't use any type hinting tools, so I have no experience with any of it. (except that it's ugly when inline except in the simplest cases))
bashtage commentedon Nov 14, 2024
The biggest challenge with statsmodels is there are too many attributes that are totally missing when not used. These should all be replaced with code like
self.attr = Noneand thenif something: self.attr = value. Statsmodels has a lot of code that useshasattrandgetattr(...,... default). Both of these are anti pattern imo.josef-pkt commentedon Nov 14, 2024
Found an interesting github package with stubs for scipy
https://github.com/jorenham/scipy-stubs/blob/master/scipy-stubs/stats/_mstats_basic.pyi
Their results classes seems to have a similar problem as our Holder class.
google search lead me to the reddit announcement
https://www.reddit.com/r/Python/comments/1gkysrl/scipystubs_type_hints_for_scipy/
the bad reason
the positive reasons
hasattrandgetattr(...,... default)look like pretty standard Python to me.#1970 #5500
jorenham commentedon Oct 3, 2025
What problem are you referring to exactly? In the last year,
scipy-stubshas now become an official SciPy project (https://github.com/scipy/scipy-stubs), and there have been quite a lot of improvements, so perhaps it's not an issue anymore?