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

feat(tag): Asher lab patch 0 #18

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion superset/commands/chart/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _file_content(model: Slice) -> str:

# Fetch tags from the database if TAGGING_SYSTEM is enabled
if feature_flag_manager.is_feature_enabled("TAGGING_SYSTEM"):
tags = model.tags if hasattr(model, "tags") else []
tags = getattr(model, "tags", [])
payload["tags"] = [tag.name for tag in tags if tag.type == TagType.custom]
file_content = yaml.safe_dump(payload, sort_keys=False)
return file_content
Expand Down
6 changes: 4 additions & 2 deletions superset/commands/chart/importers/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
# specific language governing permissions and limitations
# under the License.

from typing import Any, Optional
from __future__ import annotations

from typing import Any

from marshmallow import Schema
from sqlalchemy.orm import Session # noqa: F401
Expand Down Expand Up @@ -52,7 +54,7 @@ class ImportChartsCommand(ImportModelsCommand):
def _import(
configs: dict[str, Any],
overwrite: bool = False,
contents: Optional[dict[str, Any]] = None,
contents: dict[str, Any] | None = None,
) -> None:
if contents is None:
contents = {}
Expand Down
6 changes: 4 additions & 2 deletions superset/commands/dashboard/importers/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
# specific language governing permissions and limitations
# under the License.

from typing import Any, Optional
from __future__ import annotations

from typing import Any

from marshmallow import Schema
from sqlalchemy.orm import Session # noqa: F401
Expand Down Expand Up @@ -64,7 +66,7 @@ class ImportDashboardsCommand(ImportModelsCommand):
def _import(
configs: dict[str, Any],
overwrite: bool = False,
contents: Optional[dict[str, Any]] = None,
contents: dict[str, Any] | None = None,
) -> None:
if contents is None:
contents = {}
Expand Down
14 changes: 6 additions & 8 deletions superset/commands/export/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from superset.commands.database.export import ExportDatabasesCommand
from superset.commands.dataset.export import ExportDatasetsCommand
from superset.commands.query.export import ExportSavedQueriesCommand
from superset.commands.tag.export import ExportTagsCommand
from superset.utils.dict_import_export import EXPORT_VERSION

METADATA_FILE_NAME = "metadata.yaml"
Expand All @@ -51,17 +50,16 @@ def run(self) -> Iterator[tuple[str, Callable[[], str]]]:
ExportDatabasesCommand,
ExportDatasetsCommand,
ExportChartsCommand,
ExportTagsCommand,
ExportDashboardsCommand,
ExportSavedQueriesCommand,
]

for command in commands:
if hasattr(command, "dao"):
ids = [model.id for model in command.dao.find_all()]
for file_name, file_content in command(ids, export_related=False).run():
if file_name not in seen:
yield file_name, file_content
seen.add(file_name)
ids = [model.id for model in command.dao.find_all()]
for file_name, file_content in command(ids, export_related=False).run():
if file_name not in seen:
yield file_name, file_content
seen.add(file_name)

def validate(self) -> None:
pass
10 changes: 5 additions & 5 deletions superset/commands/importers/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Any, Optional
from __future__ import annotations

from typing import Any

from marshmallow import Schema, validate # noqa: F401
from marshmallow.exceptions import ValidationError
Expand Down Expand Up @@ -64,9 +66,7 @@ def __init__(self, contents: dict[str, str], *args: Any, **kwargs: Any):
def _import(
configs: dict[str, Any],
overwrite: bool = False,
contents: Optional[
dict[str, Any]
] = None, # Use dict[str, str] to match the parent class signature
contents: dict[str, Any] | None = None,
) -> None:
raise NotImplementedError("Subclasses MUST implement _import")

Expand All @@ -90,7 +90,7 @@ def validate(self) -> None: # noqa: F811

# verify that the metadata file is present and valid
try:
metadata: Optional[dict[str, str]] = load_metadata(self.contents)
metadata: dict[str, str] | None = load_metadata(self.contents)
except ValidationError as exc:
exceptions.append(exc)
metadata = None
Expand Down
17 changes: 9 additions & 8 deletions superset/commands/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,15 @@ def import_tag(
if "tags.yaml" in contents:
try:
tags_config = yaml.safe_load(contents["tags.yaml"])
for tag_info in tags_config.get("tags", []):
tag_name = tag_info.get("tag_name")
description = tag_info.get("description", None)
if tag_name:
tag_descriptions[tag_name] = description
except yaml.YAMLError as err: # Renamed to 'err' for clarity
logger.error("Error parsing tags.yaml: %s", err) # Used lazy logging

except yaml.YAMLError as err:
logger.error("Error parsing tags.yaml: %s", err)
tags_config = {}

for tag_info in tags_config.get("tags", []):
tag_name = tag_info.get("tag_name")
description = tag_info.get("description", None)
if tag_name:
tag_descriptions[tag_name] = description
existing_tags = (
db_session.query(TaggedObject)
.filter_by(object_id=object_id, object_type=object_type)
Expand Down
2 changes: 0 additions & 2 deletions superset/commands/tag/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from collections.abc import Iterator

import yaml
from superset.daos.tag import TagDAO
from superset.daos.chart import ChartDAO
from superset.daos.dashboard import DashboardDAO
from superset.extensions import feature_flag_manager
Expand All @@ -31,7 +30,6 @@

# pylint: disable=too-few-public-methods
class ExportTagsCommand:
dao = TagDAO
not_found = TagNotFoundError

@staticmethod
Expand Down
22 changes: 1 addition & 21 deletions tests/unit_tests/commands/export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,6 @@ def test_export_assets_command(mocker: MockerFixture) -> None:
"""
from superset.commands.export.assets import ExportAssetsCommand

ExportTagsCommand = mocker.patch(
"superset.commands.export.assets.ExportTagsCommand"
)
ExportTagsCommand.return_value.run.return_value = [
(
"metadata.yaml",
lambda: yaml.dump(
{
"tags": [
{"tag_name": "tag_1", "description": "Description for tag_1"}
]
},
sort_keys=False,
),
),
("tags.yaml", lambda: "<TAG CONTENTS>"),
]
ExportDatabasesCommand = mocker.patch(
"superset.commands.export.assets.ExportDatabasesCommand"
)
Expand Down Expand Up @@ -111,17 +94,14 @@ def test_export_assets_command(mocker: MockerFixture) -> None:
("databases/example.yaml", "<DATABASE CONTENTS>"),
("datasets/example/dataset.yaml", "<DATASET CONTENTS>"),
("charts/pie.yaml", "<CHART CONTENTS>"),
("tags.yaml", "<TAG CONTENTS>"),
("dashboards/sales.yaml", "<DASHBOARD CONTENTS>"),
("queries/example/metric.yaml", "<SAVED QUERY CONTENTS>"),
]


@pytest.fixture
def mock_export_tags_command_charts_dashboards(mocker):
ExportTagsCommand = mocker.patch(
"superset.commands.export.assets.ExportTagsCommand"
)
ExportTagsCommand = mocker.patch("superset.commands.tag.export.ExportTagsCommand")

def _mock_export(dashboard_ids=None, chart_ids=None):
if not feature_flag_manager.is_feature_enabled("TAGGING_SYSTEM"):
Expand Down
Loading