-
Notifications
You must be signed in to change notification settings - Fork 119
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
fix: rework ops.main type hints to allow different flavours (callable module) #1331
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
85d23e9
newer style annotations
dimaqq 66d34f1
use keep-runtime-typing
dimaqq 2a2367b
newer style annotations
dimaqq 871ce7c
use keep-runtime-typing
dimaqq 46c6d00
fix: rework ops.main type hints to allow different import flavours
dimaqq 1245ea4
mock things correctly
dimaqq 1161866
newer style annotations
dimaqq 1f1716a
fix: rework ops.main type hints to allow different import flavours
dimaqq 3aef186
test: split up #1331 tests
dimaqq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
from pathlib import Path | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
import ops | ||
|
||
|
||
@pytest.fixture | ||
def charm_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path): | ||
monkeypatch.setattr('sys.argv', ('hooks/install',)) | ||
monkeypatch.setattr('ops._main._emit_charm_event', Mock()) | ||
monkeypatch.setattr('ops._main._Manager._setup_root_logging', Mock()) | ||
monkeypatch.setattr('ops.charm._evaluate_status', Mock()) | ||
monkeypatch.setenv('JUJU_CHARM_DIR', str(tmp_path)) | ||
monkeypatch.setenv('JUJU_UNIT_NAME', 'test_main/0') | ||
monkeypatch.setenv('JUJU_MODEL_NAME', 'mymodel') | ||
monkeypatch.setenv('JUJU_DISPATCH_PATH', 'hooks/install') | ||
monkeypatch.setenv('JUJU_VERSION', '3.5.0') | ||
(tmp_path / 'metadata.yaml').write_text('name: test', encoding='utf-8') | ||
(tmp_path / 'dispatch').absolute().touch(mode=0o755) | ||
|
||
yield | ||
|
||
os.environ.pop('OPERATOR_DISPATCH', None) | ||
|
||
|
||
class IdleCharm(ops.CharmBase): | ||
def __init__(self, framework: ops.Framework): | ||
super().__init__(framework) | ||
|
||
|
||
def test_top_level_import(charm_env: None): | ||
import ops | ||
|
||
ops.main(IdleCharm) | ||
|
||
with pytest.raises(TypeError): | ||
ops.main() # type: ignore | ||
|
||
|
||
def test_top_level_import_legacy_call(charm_env: None): | ||
import ops | ||
|
||
ops.main.main(IdleCharm) | ||
|
||
with pytest.raises(TypeError): | ||
ops.main.main() # type: ignore | ||
|
||
|
||
def test_submodule_import(charm_env: None): | ||
import ops.main | ||
|
||
ops.main(IdleCharm) # type: ignore https://github.com/microsoft/pyright/issues/8830 | ||
|
||
with pytest.raises(TypeError): | ||
ops.main() # type: ignore | ||
|
||
|
||
def test_submodule_import_legacy_call(charm_env: None): | ||
import ops.main | ||
|
||
ops.main.main(IdleCharm) | ||
|
||
with pytest.raises(TypeError): | ||
ops.main.main() # type: ignore | ||
|
||
|
||
def test_import_from_top_level_module(charm_env: None): | ||
from ops import main | ||
|
||
main(IdleCharm) | ||
|
||
with pytest.raises(TypeError): | ||
main() # type: ignore | ||
|
||
|
||
def test_import_from_top_level_module_legacy_call(charm_env: None): | ||
from ops import main | ||
|
||
main.main(IdleCharm) | ||
|
||
with pytest.raises(TypeError): | ||
main.main() # type: ignore | ||
|
||
|
||
def test_legacy_import_from_submodule(charm_env: None): | ||
from ops.main import main | ||
|
||
main(IdleCharm) | ||
|
||
with pytest.raises(TypeError): | ||
main() # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Callable, Type | ||
|
||
import ops | ||
|
||
|
||
def type_test_dummy(_arg: Callable[[Type[ops.CharmBase], bool], None]): | ||
""" | ||
Helper to verify that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like maybe there were meant to be more words there? |
||
Usage: | ||
|
||
from somewhere import main | ||
|
||
type_test_dummy(main) | ||
""" | ||
|
||
|
||
def type_test_negative(_arg: Callable[[], None]): | ||
""" | ||
Usage: | ||
|
||
from somewhere import main | ||
|
||
type_test_negative(main) # type: ignore | ||
|
||
The `reportUnnecessaryTypeIgnoreComment` setting is expected to kick up a fuss, | ||
should the passed argument match the expected argument type. | ||
""" | ||
|
||
|
||
def top_level_import(): | ||
import ops | ||
|
||
type_test_dummy(ops.main.__call__) # pyright is quirky | ||
type_test_dummy(ops.main.main) | ||
|
||
type_test_negative(ops.main.__call__) # type: ignore | ||
type_test_negative(ops.main.main) # type: ignore | ||
|
||
|
||
def submodule_import(): | ||
import ops.main | ||
|
||
type_test_dummy(ops.main.__call__) # type: ignore https://github.com/microsoft/pyright/issues/8830 | ||
type_test_dummy(ops.main.main) | ||
|
||
type_test_negative(ops.main.__call__) # type: ignore | ||
type_test_negative(ops.main.main) # type: ignore | ||
|
||
|
||
def import_from_top_level_module(): | ||
from ops import main | ||
|
||
type_test_dummy(main.__call__) | ||
type_test_dummy(main.main) | ||
|
||
type_test_negative(main.__call__) # type: ignore | ||
type_test_negative(main.main) # type: ignore | ||
|
||
|
||
def import_from_submodule(): | ||
from ops.main import main | ||
|
||
type_test_dummy(main) | ||
|
||
type_test_negative(main) # type: ignore |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any need to have this and not just use
CharmBase
?