Skip to content

ENH/MAINT add typing, mypy or similar #8679

Open

Description

@josef-pkt

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.

pandas:
https://github.com/pandas-dev/pandas/blob/main/pandas/_typing.py

numpy
has only a few public types in numpy.typing
more details in private https://github.com/numpy/numpy/tree/main/numpy/_typing and individual pyi files

sklearn discussion
scikit-learn/scikit-learn#16705 (comment)

Activity

  1. josef-pkt commented on Feb 20, 2023

    @josef-pkt
    MemberAuthor

    #8152 PR that add some typing support to (mainly) tsa

  2. luxedo commented on Aug 7, 2024

    @luxedo

    Any updates on this? I can help typing

  3. bashtage commented on Aug 12, 2024

    @bashtage
    Member

    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.

  4. yangdanny97 commented on Nov 13, 2024

    @yangdanny97

    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.

  5. josef-pkt commented on Nov 13, 2024

    @josef-pkt
    MemberAuthor

    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

    def __init__(self, **kwds):
            self.__dict__.update(kwds)
    

    some,especially older, functions are easier because they return for example float or ndarray

    brief 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))

  6. bashtage commented on Nov 14, 2024

    @bashtage
    Member

    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.

  7. josef-pkt commented on Nov 14, 2024

    @josef-pkt
    MemberAuthor

    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/

    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..

    #1970 #5500

  8. jorenham commented on Oct 3, 2025

    @jorenham
    Contributor

    Found an interesting github package with stubs for scipy
    jorenham/scipy-stubs@master/scipy-stubs/stats/_mstats_basic.pyi
    Their results classes seems to have a similar problem as our Holder class.

    What problem are you referring to exactly? In the last year, scipy-stubs has 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions