Skip to content

TYP: Annotate fitted_values, estimated_effects and idiosyncratic as D… - #842

Merged
bashtage merged 4 commits into
mainfrom
lm71-typ1-annotations
Oct 15, 2026
Merged

bashtage merged 4 commits into
mainfrom
lm71-typ1-annotations

Conversation

@bashtage

Copy link
Copy Markdown
Owner

…ataFrame

The three properties of PanelResults were annotated as Series, but every
panel estimator builds them as one-column DataFrames (named "fitted_values",
"estimated_effects" and "idiosyncratic"), and predict concatenates them along
the columns. Annotate them with what they return.

Annotations only; no behavior change.

…ataFrame

The three properties of PanelResults were annotated as Series, but every
panel estimator builds them as one-column DataFrames (named "fitted_values",
"estimated_effects" and "idiosyncratic"), and predict concatenates them along
the columns. Annotate them with what they return.

Annotations only; no behavior change.
…ights

Every array_like parameter converts a list or a tuple to an array on entry,
but ArrayLike and IVDataLike only listed ndarray, Series, DataFrame and
DataArray. Add list and tuple to ArrayLike, which IVDataLike builds on.

PanelDataLike was built on ArrayLike. It describes dependent and exog, which
do not accept a list or a tuple since the layout of the data would not be
known, so it now uses a new DataContainer alias (ndarray, Series, DataFrame
and DataArray). PanelArrayLike (PanelData or ArrayLike) is for the panel
inputs that are array_like: weights, other_effects and clusters.

Other changes that follow from this:

* as_array_like is overloaded, so that type checkers know that it returns an
  ndarray for a list or a tuple and the container for a container. Before,
  it returned Any, and the parameter kept the type that was declared, which
  includes the list and the tuple that had been converted.
* get_array_like and get_panel_data_like return containers only, since they
  reject a list.
* in_2core_graph and in_2core_graph_slow need an array with a shape, so they
  are annotated as taking a DataFrame or an ndarray, which is what their
  docstrings say.
* predict of the panel and IV results is annotated as the model methods are:
  PanelDataLike for the panel exog, and IVDataLike for exog and endog and
  DataFrame for the data of a formula in the IV results.
* flake8 ignores E704 in shared/inputs.py, as it does in the other modules
  that have overloads.

Annotations only; no behavior change.
* entity_ids and time_ids of PanelData are integer arrays of any width (the
  codes of a categorical can be int8, int16 or int32), and are annotated as
  AnyIntArray, but the covariance estimators, panel_structure_stats and the
  list of effects in the fast path annotated them as IntArray (np.int_).
* IVData.ndarray is float64, except for the categoricals that the absorbing
  estimator keeps as they are, which it only reads through .pandas. It was
  annotated as NumericArray, so that every use as float64 data was an error
  for a type checker. Annotate it as Float64Array.
* NumericArray listed signed and unsigned integers separately, which type
  checkers do not match to the integer type that NumPy functions return.
  Use the integer type.
* IVSystemGMM.fit and _finalize_results annotated the covariance options as
  bool or float, but the documented ``kernel`` option is a str.

Annotations only; no behavior change.
mypy reports these assignments because the first assignment of a name fixes
its type: an integer array that becomes a float array, a 2-d array that is
replaced by an array of any shape, a float that becomes an array or NaN. Put
the annotation that the later assignments need on the first one.

Also:

* the compat module for SciPy assigns the matrix classes to the names of the
  array classes when SciPy 1 is installed, and shared.inputs assigns
  np.VisibleDeprecationWarning when NumPy is older than 1.25. The stubs for
  SciPy 2 and NumPy 2 reject both, so each assignment has a type: ignore
  with its error codes and a comment that says why.
* the estimator of the first-stage score covariance is one of three weight
  matrix classes, which are not subclasses of each other.
* _validate_sigma returns a 2-d array, so it is annotated as one.

Annotations and comments only; no behavior change.
if TYPE_CHECKING:
from xarray import DataArray

from linearmodels.iv.data import IVData
from xarray import DataArray

from linearmodels.iv.data import IVData
from linearmodels.panel.data import PanelData
from pandas.api.types import is_complex_dtype

if TYPE_CHECKING:
from xarray import DataArray
if TYPE_CHECKING:
from xarray import DataArray

from linearmodels.iv.data import IVData
from xarray import DataArray

from linearmodels.iv.data import IVData
from linearmodels.panel.data import PanelData
@overload
def as_array_like( # numpydoc ignore=GL08
value: list[Any] | tuple[Any, ...], name: str
) -> np.ndarray: ...
@overload
def as_array_like( # numpydoc ignore=GL08
value: _Container, name: str
) -> _Container: ...
@bashtage
bashtage merged commit 6211e31 into main Oct 15, 2026
12 of 22 checks passed
@bashtage
bashtage deleted the lm71-typ1-annotations branch October 15, 2026 11:40
@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.68%. Comparing base (068bbb9) to head (c64d0e1).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
linearmodels/shared/inputs.py 66.66% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #842      +/-   ##
==========================================
- Coverage   99.68%   99.68%   -0.01%     
==========================================
  Files         198      198              
  Lines       37694    37703       +9     
  Branches     2426     2428       +2     
==========================================
+ Hits        37577    37584       +7     
  Misses         51       51              
- Partials       66       68       +2     
Flag Coverage Δ
adder 99.67% <95.00%> (-0.01%) ⬇️
subtractor 99.67% <95.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants