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

Add missing __init__.py files #1367

Merged
merged 12 commits into from Jun 22, 2020
Merged

Conversation

harupy
Copy link
Contributor

@harupy harupy commented Jun 14, 2020

Motivation

mypy ignores directories that don't contain __init__.py.

Description of the changes

Add __init__.py files.

I ran the test below to make sure all the directories contain __init__.py

import os


def test_init_exists_in_all_directories() -> None:
    dirs = []
    for top in ["optuna", "tests"]:
        for root, _, files in os.walk(top):
            if any(f.endswith(".py") for f in files) and "__init__.py" not in files:
                dirs.append(root)
    assert len(dirs) == 0, "Found directories that don't contain `__init__.py`: {}".format(dirs)

@harupy
Copy link
Contributor Author

harupy commented Jun 15, 2020

mypy check failed:

tests/multi_objective/test_trial.py:25: error: Unsupported operand types for + ("float" and "None")
tests/multi_objective/test_trial.py:25: error: Unsupported operand types for + ("float" and "str")
tests/multi_objective/test_trial.py:25: note: Right operand is of type "Union[None, bool, int, float, str]"
tests/multi_objective/test_trial.py:35: error: Argument 1 to "report" of "MultiObjectiveTrial" has incompatible type "Tuple[int, int, int]"; expected "Tuple[float]"
tests/multi_objective/test_trial.py:36: error: Argument 1 to "report" of "MultiObjectiveTrial" has incompatible type "Tuple[int, int, int]"; expected "Tuple[float]"
tests/multi_objective/test_trial.py:150: error: Value expression in dictionary comprehension has incompatible type "Optional[float]"; expected type "float"
tests/multi_objective/samplers_tests/test_nsga2.py:41: error: Argument "population_size" to "NSGAIIMultiObjectiveSampler" has incompatible type "float"; expected "int"
optuna/storages/rdb/alembic/env.py:32: error: Function is missing a return type annotation
optuna/storages/rdb/alembic/env.py:32: note: Use "-> None" if function does not return a value
optuna/storages/rdb/alembic/env.py:51: error: Function is missing a return type annotation
optuna/storages/rdb/alembic/env.py:51: note: Use "-> None" if function does not return a value
tests/multi_objective/test_study.py:131: error: Incompatible return value type (got "Tuple[float, float]", expected "List[float]")
Found 9 errors in 4 files (checked 171 source files)

@harupy
Copy link
Contributor Author

harupy commented Jun 15, 2020

tests/multi_objective/test_trial.py:35: error: Argument 1 to "report" of "MultiObjectiveTrial" has incompatible type "Tuple[int, int, int]"; expected "Tuple[float]"
tests/multi_objective/test_trial.py:36: error: Argument 1 to "report" of "MultiObjectiveTrial" has incompatible type "Tuple[int, int, int]"; expected "Tuple[float]"

It seems the type annotation of values in multi_objective should be fixed. It is annotated as Tuple[float] which represents a single-element tuple, but it should be Tuple[float, ...] which represents a variable-length tuple.

def report(self, values: Tuple[float], step: int) -> None:

@hvy
Copy link
Member

hvy commented Jun 15, 2020

Good catch! We should address the failures in the same PR. Taking a brief look through the errors, they mostly seem straight forward to address (e.g. by introducing assertions). Do you want to do that here? I otherwise suggest we split up this PR into multiple parts.

@hvy hvy added the code-fix Change that does not change the behavior, such as code refactoring. label Jun 15, 2020
@hvy hvy self-assigned this Jun 15, 2020
@toshihikoyanase
Copy link
Member

Thank you for finding the missing type check.

I think optuna/storages/rdb/alembic/ is a kind of settings for alembic rather than a part of Optuna's submodules (i.e., Optuna doesn't import it as a module.) It may be controversial to include the alembic submodule only for mypy.

What do you think?

@harupy
Copy link
Contributor Author

harupy commented Jun 15, 2020

@hvy Got it. I'll fix all the errors in this PR.

@toshihikoyanase If so, I'll make mypy ignore files under alembic.

@toshihikoyanase toshihikoyanase self-assigned this Jun 15, 2020
p0 = trial.suggest_float("p0", -10, 10)
p1 = trial.suggest_uniform("p1", 3, 5)
p2 = trial.suggest_loguniform("p2", 0.00001, 0.1)
p3 = trial.suggest_discrete_uniform("p3", 100, 200, q=5)
p4 = trial.suggest_int("p4", -20, -15)
p5 = trial.suggest_categorical("p5", [7, 1, 100])
p5 = cast(int, trial.suggest_categorical("p5", [7, 1, 100]))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@harupy harupy Jun 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an idea. It'd be nice if we could infer the return type of suggest_categorical in a generic way like "return int if int list given" instead of returning Union of several python primitive types because this enforces users to cast a suggested value to resolve type errors when they use Optuna with a type-checking tool like mypy.

@harupy
Copy link
Contributor Author

harupy commented Jun 16, 2020

The tests in tests/test_cli.py failed.

@toshihikoyanase
Copy link
Member

@harupy I found similar issues in other PRs (e.g., https://circleci.com/gh/optuna/optuna/45221?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link) and daily CI (https://github.com/optuna/optuna/actions/runs/136123282). I'm sorry but I'm under investigation.

@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2020

Codecov Report

Merging #1367 into master will increase coverage by 0.14%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1367      +/-   ##
==========================================
+ Coverage   87.92%   88.06%   +0.14%     
==========================================
  Files          98       97       -1     
  Lines        7376     7297      -79     
==========================================
- Hits         6485     6426      -59     
+ Misses        891      871      -20     
Impacted Files Coverage Δ
optuna/multi_objective/study.py 97.36% <100.00%> (+0.02%) ⬆️
optuna/multi_objective/trial.py 91.21% <100.00%> (-0.06%) ⬇️
optuna/trial/_base.py 56.36% <0.00%> (-2.26%) ⬇️
optuna/integration/lightgbm_tuner/optimize.py 91.82% <0.00%> (-0.26%) ⬇️
optuna/study.py 94.71% <0.00%> (-0.17%) ⬇️
optuna/_study_summary.py 100.00% <0.00%> (ø)
optuna/samplers/cmaes.py
optuna/storages/redis.py
optuna/storages/base.py
optuna/visualization/param_importances.py
... and 64 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5940731...943792c. Read the comment docs.

Copy link
Member

@hvy hvy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we allow both tuples and lists (sequence) giving the user some flexibility in how to design their objective functions? It's cumbersome having to convert lists to tuples. This is not a strong opinion though.

@harupy
Copy link
Contributor Author

harupy commented Jun 16, 2020

That makes sense. I'll fix the type annotaions.

@harupy
Copy link
Contributor Author

harupy commented Jun 22, 2020

@hvy I have fixed the type annotations to use Sequence instead of Tuple. Could you review the code again?

Copy link
Member

@hvy hvy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed response, and thanks for fixing the details. LGTM!

@hvy
Copy link
Member

hvy commented Jun 22, 2020

@toshihikoyanase could you cont. for the second approval?

Copy link
Member

@toshihikoyanase toshihikoyanase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you for your contribution!

@hvy Thank you for letting me know.

@toshihikoyanase toshihikoyanase merged commit 35a8d7e into optuna:master Jun 22, 2020
@toshihikoyanase toshihikoyanase added this to the v2.0.0 milestone Jun 22, 2020
@harupy harupy deleted the add-missing-init branch June 22, 2020 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code-fix Change that does not change the behavior, such as code refactoring.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants