Skip to content

Instantly share code, notes, and snippets.

@mitar
Last active June 29, 2020 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitar/a9ac3c84c5dafe9f9805ecc75cab747d to your computer and use it in GitHub Desktop.
Save mitar/a9ac3c84c5dafe9f9805ecc75cab747d to your computer and use it in GitHub Desktop.
sklearn base
try:
from typing import Annotated
except ImportError:
# Backported Annotated, available through https://pypi.org/project/typing-extensions/.
from typing_extensions import Annotated
class Model(Base):
# This docstring is automatically extended with types and defaults.
# Maybe one of the following existing projects could be reused for this:
# https://github.com/agronholm/sphinx-autodoc-typehints
# https://www.sphinx-doc.org/en/2.0/usage/extensions/autodoc.html#generating-documents-from-type-annotations
# A similar approach could be used to extract default values out as well.
"""This is a cool model.
Parameters
----------
penalty:
This is a something cool.
- This is a list
- Another list
n_jobs:
This is parallel.
Attributes
----------
n_features_:
The number of features when ``fit`` is performed.
estimators_:
The collection of fitted sub-estimators.
"""
# So this allows mypy and IDEs to know which properties are available on the object.
n_features_: int
estimators_: List[sklearn.tree.DecisionTreeClassifier]
# If there has to be any special handling of default values for backwards compatibility reasons,
# or some other special processing where it is imporant to know if parameter was passed in or not,
# then we should use a decorator to help with that, but keep the original constructor signature.
def __init__(self, *,
penalty: Annotated[str, Enum("l2", "l1", "elasticnet", "none")] = "l2",
n_jobs: Annotated[Union[int, str], Interval(int, lower=1) + Enum(-1, None), Tags('resource')] = None,
):
self.penalty = penalty
self.n_jobs = n_jobs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment