Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add typing for np.random and Generator #17103

Closed
bashtage opened this issue Aug 19, 2020 · 8 comments
Closed

ENH: Add typing for np.random and Generator #17103

bashtage opened this issue Aug 19, 2020 · 8 comments

Comments

@bashtage
Copy link
Contributor

NumPy supplies partial typing support. Currently random's methods and Generator do not have typing information which results in errors in mypy.

Reproducing code example:

Use mypy on any project that uses np.random and you will see output like

linearmodels/tests/iv/results/simulated-test-data.py:16: error: Module 'numpy.random' has no attribute 'multivariate_normal'
linearmodels/tests/iv/results/simulated-test-data.py:16: error: Module 'numpy.random' has no attribute 'seed'
linearmodels/tests/iv/results/simulated-test-data.py:69: error: Module has no attribute "chisquare"
linearmodels/panel/utility.py:496: error: Name 'np.random.RandomState' is not defined
linearmodels/panel/utility.py:534: error: Module has no attribute "RandomState"; maybe "__RandomState_ctor"?

Numpy/Python version information:

1.19.1 3.7.7 (default, May 7 2020, 21:25:33)
[GCC 7.3.0]

@mattip
Copy link
Member

mattip commented Aug 19, 2020

Does the situation change on HEAD?

@bashtage
Copy link
Contributor Author

bashtage commented Aug 19, 2020

edit: Remove linalg issues that seem to be spurious.

Random is about the same.

np.random issues

linearmodels\panel\utility.py:506: error: Name 'np.random.RandomState' is not defined
linearmodels\panel\utility.py:544: error: Module has no attribute "RandomState"
linearmodels\tests\iv\test_data.py:69: error: Module has no attribute "randn"
linearmodels\tests\iv\test_data.py:89: error: Module has no attribute "randn"
linearmodels\tests\iv\results\simulated-test-data.py:16: error: Module 'numpy.random' has no attribute 'multivariate_normal'
linearmodels\tests\iv\results\simulated-test-data.py:16: error: Module 'numpy.random' has no attribute 'seed'
linearmodels\tests\iv\results\simulated-test-data.py:69: error: Module has no attribute "chisquare"
linearmodels\tests\system\results\generate_data.py:20: error: Module has no attribute "seed"
linearmodels\tests\system\results\generate_data.py:23: error: Module has no attribute "random_sample"
linearmodels\tests\system\results\generate_data.py:27: error: Module has no attribute "random_sample"
linearmodels\tests\panel\_utility.py:3: error: Module 'numpy.random' has no attribute 'standard_normal'
linearmodels\tests\panel\results\generate-panel-data.py:9: error: Module has no attribute "seed"
linearmodels\tests\panel\results\generate-panel-data.py:11: error: Module has no attribute "randn"
linearmodels\tests\panel\results\generate-panel-data.py:14: error: Module has no attribute "randn"
linearmodels\tests\panel\results\generate-panel-data.py:17: error: Module has no attribute "randn"
linearmodels\tests\panel\results\generate-panel-data.py:18: error: Module has no attribute "chisquare"
linearmodels\tests\panel\results\generate-panel-data.py:49: error: Module has no attribute "choice"
linearmodels\tests\panel\results\generate-panel-data.py:57: error: Module has no attribute "choice"
linearmodels\tests\panel\test_panel_covariance.py:34: error: Module has no attribute "random_sample"
linearmodels\tests\panel\test_panel_covariance.py:35: error: Module has no attribute "random_sample"
linearmodels\tests\panel\test_panel_covariance.py:43: error: Module has no attribute "randint"
linearmodels\tests\panel\test_panel_covariance.py:44: error: Module has no attribute "randint"
linearmodels\tests\panel\test_panel_covariance.py:45: error: Module has no attribute "randint"

flatiter issues

linearmodels\panel\model.py:2145: error: Value of type "flatiter[ndarray]" is not indexable
linearmodels\panel\model.py:2145: error: Argument 1 to "__call__" of "ufunc" has incompatible type "flatiter[ndarray]"; expected "Union[bool, int, float, complex, _SupportsArray, Sequence[Any]]"

@BvB93
Copy link
Member

BvB93 commented Aug 19, 2020

#17104 should more or less fix the issue by adding placeholder stub files for all (untyped) sub-modules.
Note that all objects within aforementioned modules are typed as Any.

More generally speaking, there is a large list of to-be typed objects, (see #16546) and this is simply going to take quite a bit time.
If you'd be willing to help out then that would be very much appreciated.

@BvB93
Copy link
Member

BvB93 commented Sep 8, 2020

#17104 has been merged now; all (public) objects in np.random are currently annotated as Any.

While still very broad, this will silence all "Module has no attribute ..." errors.

@jinwuxia
Copy link

jinwuxia commented Feb 4, 2021

#17104 has been merged now; all (public) objects in np.random are currently annotated as Any.

While still very broad, this will silence all "Module has no attribute ..." errors.

I am not sure whether adding these Any types for silencing Mypy errors is a good practice?
My meaning is that type annotation aims to help understand code and static check type errors, but any types take no such role.

I just feel contradictory about this.
But I don't have any other ideas to quickly remove "Module has no attribute ..." errors.

@BvB93
Copy link
Member

BvB93 commented Feb 4, 2021

I am not sure whether adding these Any types for silencing Mypy errors is a good practice?

Considering the typing of numpy is very much a work in progress, I don't see any problem with this:
The Anys will naturally disappear as more annotations will be added over the course of time.

Improvements are of course possible and, in fact, would be very much welcome additions.

@bashtage
Copy link
Contributor Author

bashtage commented Feb 4, 2021

I agree that this is the pragmatic approach that allows for improvement when people who rely on these modules have time to invest.

@BvB93
Copy link
Member

BvB93 commented Feb 24, 2021

Closed by #18433 and #18395.

@BvB93 BvB93 closed this as completed Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants