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

replace wildcard imports with explicit import statements; upgrade black to v20.8b1 #36

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- id: black
language_version: python3
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ channels:
- astropy
- conda-forge
dependencies:
- black = 19.*
- black = 20.8b1
- boruta_py = 0.3.*
- conda-build
- conda-verify
Expand Down
12 changes: 3 additions & 9 deletions src/sklearndf/_sklearndf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from abc import ABCMeta, abstractmethod
from typing import *
from typing import Any, List, Mapping, Optional, Sequence, Type, TypeVar, Union, cast

import pandas as pd
from sklearn.base import (
Expand All @@ -19,13 +19,7 @@

log = logging.getLogger(__name__)

__all__ = [
"EstimatorDF",
"LearnerDF",
"ClassifierDF",
"RegressorDF",
"TransformerDF",
]
__all__ = ["EstimatorDF", "LearnerDF", "ClassifierDF", "RegressorDF", "TransformerDF"]

#
# type variables
Expand Down Expand Up @@ -401,4 +395,4 @@ def classes_(self) -> Sequence[Any]:

:return: the classes predicted by this classifier
"""
pass
pass
18 changes: 16 additions & 2 deletions src/sklearndf/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@
import re
from abc import ABCMeta, abstractmethod
from functools import update_wrapper
from typing import *
from typing import (
Optional,
Iterable,
Generic,
Mapping,
Sequence,
Any,
Union,
Callable,
TypeVar,
Type,
AnyStr,
Dict,
List,
)

import numpy as np
import pandas as pd
Expand All @@ -29,7 +43,7 @@
)

from pytools.api import inheritdoc
from sklearndf import (ClassifierDF, EstimatorDF, LearnerDF, RegressorDF, TransformerDF)
from sklearndf import ClassifierDF, EstimatorDF, LearnerDF, RegressorDF, TransformerDF

log = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion src/sklearndf/classification/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import logging
from abc import ABCMeta
from typing import *
from typing import Any, List, Optional, Sequence, Union

import numpy as np
import pandas as pd
Expand Down
2 changes: 1 addition & 1 deletion src/sklearndf/pipeline/_learner_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from abc import ABCMeta, abstractmethod
from typing import *
from typing import Any, Generic, List, Optional, Sequence, TypeVar, Union

import pandas as pd
from sklearn.base import BaseEstimator
Expand Down
2 changes: 1 addition & 1 deletion src/sklearndf/pipeline/_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from abc import ABCMeta
from typing import *
from typing import Iterator, List, Sequence, Tuple, Union, cast

import numpy as np
import pandas as pd
Expand Down
3 changes: 2 additions & 1 deletion src/sklearndf/regression/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import logging
from abc import ABCMeta
from typing import *
from typing import Any, Generic, Optional, TypeVar, Union

import pandas as pd
from sklearn.base import RegressorMixin
Expand Down Expand Up @@ -56,6 +56,7 @@

from .. import RegressorDF, TransformerDF
from .._wrapper import _MetaRegressorWrapperDF, _RegressorWrapperDF, df_estimator

# noinspection PyProtectedMember
from ..transformation._wrapper import _ColumnPreservingTransformerWrapperDF

Expand Down
7 changes: 2 additions & 5 deletions src/sklearndf/regression/_regression_v0_22.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
from sklearn 0.22 onwards
"""
import logging
from typing import *
from typing import TypeVar

from sklearn.base import RegressorMixin
from sklearn.ensemble import StackingRegressor

from .. import RegressorDF
from .._wrapper import (
_StackingRegressorWrapperDF,
df_estimator,
)
from .._wrapper import _StackingRegressorWrapperDF, df_estimator

# noinspection PyProtectedMember

Expand Down
2 changes: 1 addition & 1 deletion src/sklearndf/regression/_regression_v0_23.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import logging
from typing import *
from typing import TypeVar

from sklearn.base import RegressorMixin
from sklearn.linear_model import GammaRegressor, PoissonRegressor, TweedieRegressor
Expand Down
6 changes: 3 additions & 3 deletions src/sklearndf/transformation/_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import logging
from abc import ABCMeta
from functools import reduce
from typing import *
from typing import List
from typing import Iterable, List, TypeVar

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -228,7 +227,8 @@ def _validate_delegate_estimator(self) -> None:
if non_compliant_transformers:
raise ValueError(
f"{ColumnTransformerDF.__name__} only accepts strings or "
f"instances of " f"{TransformerDF.__name__} as valid transformers, but "
f"instances of "
f"{TransformerDF.__name__} as valid transformers, but "
f'also got: {", ".join(non_compliant_transformers)}'
)

Expand Down
2 changes: 1 addition & 1 deletion src/sklearndf/transformation/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from abc import ABCMeta, abstractmethod
from typing import *
from typing import Any, Generic, Optional, TypeVar, Union

import pandas as pd
from sklearn.base import TransformerMixin
Expand Down
7 changes: 2 additions & 5 deletions src/sklearndf/transformation/extra/_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

import logging
from abc import ABCMeta
from typing import *
from typing import Any, Mapping, Optional, TypeVar, Union

import pandas as pd
from boruta import BorutaPy
from sklearn.base import BaseEstimator

from .._wrapper import (
_ColumnSubsetTransformerWrapperDF,
_NDArrayTransformerWrapperDF,
)
from .._wrapper import _ColumnSubsetTransformerWrapperDF, _NDArrayTransformerWrapperDF
from ... import TransformerDF
from ..._wrapper import _MetaEstimatorWrapperDF, df_estimator

Expand Down
26 changes: 13 additions & 13 deletions test/test/sklearndf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import re
import sys
from distutils import version
from typing import *
from typing import Type
from typing import Any, Iterable, List, Mapping, Optional, Set, Type, Union

import pandas as pd
import sklearn
Expand All @@ -14,13 +13,14 @@
Module: type = Any


def find_all_classes(*modules: Module) -> Set[type]:
def find_all_classes(*modules: Module) -> Set[Type[_EstimatorWrapperDF]]:
""" Finds all Class members in given module/modules. """
types: Set[type] = set()
types: Set[Type[_EstimatorWrapperDF]] = set()

def _add_classes_from_module(_m: Module) -> None:
for member in vars(module).values():
if isinstance(member, type):
member: Type[_EstimatorWrapperDF]
types.add(member)

for module in modules:
Expand All @@ -40,8 +40,8 @@ def find_all_submodules(parent_module: Module) -> Set[Module]:


def sklearn_delegate_classes(
module: Module
) -> Dict[BaseEstimator, _EstimatorWrapperDF]:
module: Module,
) -> Mapping[Type[BaseEstimator], Type[_EstimatorWrapperDF]]:
""" Creates a dictionary mapping from sklearndf -> sklearn classes. """
return {
df_class.__wrapped__: df_class
Expand All @@ -54,7 +54,7 @@ def list_classes(
from_modules: Union[Module, Iterable[Module]],
matching: str,
excluding: Optional[Union[str, Iterable[str]]] = None,
) -> List[Type]:
) -> List[Type[_EstimatorWrapperDF]]:
""" Helper to return all classes with matching name from Python module(s) """

if not isinstance(from_modules, Iterable):
Expand All @@ -73,7 +73,7 @@ def list_classes(

def get_sklearndf_wrapper_class(
to_wrap: Type[BaseEstimator], from_module=None
) -> _EstimatorWrapperDF:
) -> Type[_EstimatorWrapperDF]:
""" Helper to return the wrapped counterpart for a sklearn class """
try:
return sklearn_delegate_classes(from_module)[to_wrap]
Expand All @@ -92,10 +92,10 @@ def check_expected_not_fitted_error(estimator: Union[LearnerDF, TransformerDF]):
test_x = pd.DataFrame(data=list(range(10)))

def check_sklearndf_call(
func_to_call: str, estimator: Union[LearnerDF, TransformerDF]
func_to_call: str, _estimator: Union[LearnerDF, TransformerDF]
) -> None:
try:
getattr(estimator, func_to_call)(X=test_x)
getattr(_estimator, func_to_call)(X=test_x)
except sklearn.exceptions.NotFittedError:
# This is the expected error, that sklearn[df] should raise
return
Expand All @@ -108,16 +108,16 @@ def check_sklearndf_call(
else:
x = test_x.values.reshape(-1)

getattr(estimator.native_estimator, func_to_call)(x)
getattr(_estimator.native_estimator, func_to_call)(x)
except sklearn.exceptions.NotFittedError:
raise AssertionError(
"sklearndf did not return an expected NotFittedError"
f" for {estimator.__class__.__name__}"
f" for {_estimator.__class__.__name__}"
)
except Exception as sklearn_exception:
assert repr(sklearndf_exception) == repr(sklearn_exception), (
"sklearndf raised a different error as sklearn"
f" for {estimator.__class__.__name__}:"
f" for {_estimator.__class__.__name__}:"
f"sklearndf: {repr(sklearndf_exception)} \n"
f"sklearn: {repr(sklearn_exception)}"
)
Expand Down
2 changes: 1 addition & 1 deletion test/test/sklearndf/pipeline/test_pipeline_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
from distutils.version import LooseVersion
from tempfile import mkdtemp
from typing import *
from typing import Any, Dict, Mapping

import joblib
import numpy as np
Expand Down
3 changes: 1 addition & 2 deletions test/test/sklearndf/test_classification.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from itertools import chain
from typing import *
from typing import Type

import numpy as np
import pandas as pd
# noinspection PyPackageRequirements
import pytest
from sklearn.multioutput import ClassifierChain, MultiOutputClassifier

Expand Down
8 changes: 4 additions & 4 deletions test/test/sklearndf/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import *
from typing import List, Type

import pandas as pd
# noinspection PyPackageRequirements
import pytest
from sklearn.multioutput import MultiOutputRegressor, RegressorChain

import sklearndf.regression
from sklearndf import RegressorDF, TransformerDF
from sklearndf._wrapper import _EstimatorWrapperDF
from sklearndf.regression import (
IsotonicRegressionDF,
LinearRegressionDF,
Expand All @@ -15,7 +15,7 @@
)
from test.sklearndf import check_expected_not_fitted_error, list_classes

REGRESSORS_TO_TEST: List[Type] = list_classes(
REGRESSORS_TO_TEST: List[Type[_EstimatorWrapperDF]] = list_classes(
from_modules=sklearndf.regression,
matching=r".*DF",
excluding=[RegressorDF.__name__, TransformerDF.__name__, r".*WrapperDF"],
Expand All @@ -25,7 +25,7 @@
"MultiOutputRegressorDF": {"estimator": RandomForestRegressorDF()},
"RegressorChainDF": {"base_estimator": RandomForestRegressorDF()},
"VotingRegressorDF": {
"estimators": [("rfr", RandomForestRegressorDF()), ("svmr", SVRDF())]
"estimators": [("rfr", RandomForestRegressorDF()), ("svr", SVRDF())]
},
"StackingRegressorDF": {
"estimators": (
Expand Down
9 changes: 2 additions & 7 deletions test/test/sklearndf/test_sklearn_coverage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import itertools
from typing import *
from typing import Dict
from typing import Dict, Iterable, List, Optional, Type, Union

import pytest
import sklearn
Expand All @@ -19,11 +18,7 @@
from sklearndf import EstimatorDF
from test import check_sklearn_version
from test.conftest import UNSUPPORTED_SKLEARN_PACKAGES
from test.sklearndf import (
find_all_submodules,
list_classes,
sklearn_delegate_classes,
)
from test.sklearndf import find_all_submodules, list_classes, sklearn_delegate_classes

Module = type(sklearn)

Expand Down
3 changes: 1 addition & 2 deletions test/test/sklearndf/transformation/test_imputers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import itertools
import logging
from typing import *
from typing import Type

import numpy as np
import pandas as pd
# noinspection PyPackageRequirements
import pytest

import sklearndf.transformation
Expand Down
Loading