Skip to content

Commit 386dbb4

Browse files
priyap286omkhegde
andauthored
Added a method to provide plugin information that is included in artifact zip (#167)
Co-authored-by: Omkar Hegde <[email protected]>
1 parent 0902f6f commit 386dbb4

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

.pre-commit-config.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ repos:
4949
- id: bandit
5050
files: ^(src|python)/
5151
- repo: https://github.com/pre-commit/mirrors-mypy
52-
rev: v0.711
52+
rev: v0.790
5353
hooks:
5454
- id: mypy
5555
files: ^src/
5656
args:
5757
- --strict
5858
- --implicit-reexport
59+
- --no-warn-unused-ignores # https://github.com/python/mypy/issues/8823, https://github.com/python/mypy/issues/8990
5960
- repo: local
6061
hooks:
6162
- id: pylint-local

python/rpdk/python/codegen.py

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import PurePosixPath
66
from subprocess import PIPE, CalledProcessError, run as subprocess_run # nosec
77
from tempfile import TemporaryFile
8+
from typing import Dict
89

910
import docker
1011
from docker.errors import APIError, ContainerError, ImageLoadError
@@ -15,6 +16,7 @@
1516
from rpdk.core.jsonutils.resolver import ContainerType, resolve_models
1617
from rpdk.core.plugin_base import LanguagePlugin
1718

19+
from . import __version__
1820
from .resolver import contains_model, translate_type
1921

2022
LOG = logging.getLogger(__name__)
@@ -169,6 +171,12 @@ def generate(self, project):
169171

170172
LOG.debug("Generate complete")
171173

174+
# pylint: disable=unused-argument
175+
# the argument "project" is not used here but is used in codegen.py of other plugins
176+
# this method is called in cloudformation-cli/src/rpdk/core/project.py
177+
def get_plugin_information(self, project) -> Dict:
178+
return self._get_plugin_information()
179+
172180
def _pre_package(self, build_path):
173181
f = TemporaryFile("w+b") # pylint: disable=R1732
174182

@@ -233,6 +241,10 @@ def _make_pip_command(base_path):
233241
str(base_path / "build"),
234242
]
235243

244+
@staticmethod
245+
def _get_plugin_information() -> Dict:
246+
return {"plugin-tool-version": __version__, "plugin-name": "python"}
247+
236248
@classmethod
237249
def _docker_build(cls, external_path):
238250
internal_path = PurePosixPath("/project")

src/cloudformation_cli_python_lib/recast.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import typing
23
from typing import Any, Dict, List, Mapping, Set
34

@@ -14,7 +15,7 @@ def recast_object(
1415
if not isinstance(json_data, dict):
1516
raise InvalidRequest(f"Can only parse dict items, not {type(json_data)}")
1617
# if type is Any, we leave it as is
17-
if cls == typing.Any:
18+
if cls is typing.Any:
1819
return
1920
for k, v in json_data.items():
2021
if isinstance(v, dict):
@@ -35,7 +36,7 @@ def recast_object(
3536

3637
def _recast_lists(cls: Any, k: str, v: List[Any], classes: Dict[str, Any]) -> List[Any]:
3738
# Leave as is if type is Any
38-
if cls == typing.Any:
39+
if cls is typing.Any:
3940
return v
4041
if "__dataclass_fields__" not in dir(cls):
4142
pass
@@ -64,7 +65,7 @@ def cast_sequence_item(cls: Any, k: str, item: Any, classes: Dict[str, Any]) ->
6465

6566

6667
def _recast_primitive(cls: Any, k: str, v: Any) -> Any:
67-
if cls == typing.Any:
68+
if cls is typing.Any:
6869
# If the type is Any, we cannot guess what the original type was, so we leave
6970
# it as a string
7071
return v
@@ -126,6 +127,6 @@ def get_forward_ref_type() -> Any:
126127
# ignoring mypy on the import as it catches (_)ForwardRef as invalid, use for
127128
# introspection is valid:
128129
# https://docs.python.org/3/library/typing.html#typing.ForwardRef
129-
if "ForwardRef" in dir(typing):
130-
return typing.ForwardRef # type: ignore
131-
return typing._ForwardRef # type: ignore
130+
if sys.version_info < (3, 7):
131+
return typing._ForwardRef # type: ignore
132+
return typing.ForwardRef

tests/lib/recast_test.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,15 @@ class SevenType:
148148

149149

150150
def test_get_forward_ref_type():
151-
with patch("cloudformation_cli_python_lib.recast.typing") as mock_typing:
152-
mock_typing.ForwardRef = "3.7+"
153-
assert get_forward_ref_type() == "3.7+"
154-
with patch("cloudformation_cli_python_lib.recast.typing") as mock_typing:
155-
mock_typing._ForwardRef = "3.6"
156-
get_forward_ref_type()
157-
assert get_forward_ref_type() == "3.6"
151+
with patch("cloudformation_cli_python_lib.recast.sys") as mock_sys:
152+
mock_sys.version_info = (3, 7)
153+
with patch("cloudformation_cli_python_lib.recast.typing") as mock_typing:
154+
mock_typing.ForwardRef = "3.7+"
155+
assert get_forward_ref_type() == "3.7+"
156+
157+
with patch("cloudformation_cli_python_lib.recast.sys") as mock_sys:
158+
mock_sys.version_info = (3, 6)
159+
with patch("cloudformation_cli_python_lib.recast.typing") as mock_typing:
160+
mock_typing._ForwardRef = "3.6"
161+
get_forward_ref_type()
162+
assert get_forward_ref_type() == "3.6"

tests/plugin/codegen_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from requests.exceptions import ConnectionError as RequestsConnectionError
1515
from rpdk.core.exceptions import DownstreamError
1616
from rpdk.core.project import Project
17+
from rpdk.python.__init__ import __version__
1718
from rpdk.python.codegen import (
1819
SUPPORT_LIB_NAME,
1920
SUPPORT_LIB_PKG,
@@ -289,6 +290,13 @@ def test__docker_build_good_path(plugin, tmp_path):
289290
)
290291

291292

293+
def test_get_plugin_information(project):
294+
plugin_information = project._plugin.get_plugin_information(project)
295+
296+
assert plugin_information["plugin-tool-version"] == __version__
297+
assert plugin_information["plugin-name"] == "python"
298+
299+
292300
@pytest.mark.parametrize(
293301
"exception",
294302
[

0 commit comments

Comments
 (0)