-
Notifications
You must be signed in to change notification settings - Fork 3k
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(sdk): add scaffolding for sdk v2 #12554
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cd50820
copy out files
hsheth2 4720daa
remove stuff that wasn't ready for prime time
hsheth2 3269df5
entity client tweaks
hsheth2 7433575
add resolver client
hsheth2 543cefe
add dataset sdk tests
hsheth2 555fe22
fix platform instance generation
hsheth2 8fe3bcc
fix lint
hsheth2 5f805ba
beef up dataset tests
hsheth2 9b96623
use ...
hsheth2 82dcf63
add tests/unit/sdk_v2/test_entity_client.py tests
hsheth2 31997a6
add client tests
hsheth2 0e46346
add resolver test
hsheth2 3b9bfda
fix mypy error
hsheth2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from datahub.configuration.common import MetaError | ||
|
||
# TODO: Move all other error types to this file. | ||
|
||
|
||
class SdkUsageError(MetaError): | ||
pass | ||
|
||
|
||
class AlreadyExistsError(SdkUsageError): | ||
pass | ||
|
||
|
||
class ItemNotFoundError(SdkUsageError): | ||
pass | ||
|
||
|
||
class MultipleItemsFoundError(SdkUsageError): | ||
pass | ||
|
||
|
||
class SchemaFieldKeyError(SdkUsageError, KeyError): | ||
pass | ||
|
||
|
||
class IngestionAttributionWarning(Warning): | ||
pass | ||
|
||
|
||
class MultipleSubtypesWarning(Warning): | ||
pass | ||
|
||
|
||
class ExperimentalWarning(Warning): | ||
pass |
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,33 @@ | ||
import warnings | ||
|
||
import datahub.metadata.schema_classes as models | ||
from datahub.errors import ExperimentalWarning, SdkUsageError | ||
from datahub.ingestion.graph.config import DatahubClientConfig | ||
from datahub.metadata.urns import ( | ||
ChartUrn, | ||
ContainerUrn, | ||
CorpGroupUrn, | ||
CorpUserUrn, | ||
DashboardUrn, | ||
DataPlatformInstanceUrn, | ||
DataPlatformUrn, | ||
DatasetUrn, | ||
DomainUrn, | ||
GlossaryTermUrn, | ||
SchemaFieldUrn, | ||
TagUrn, | ||
) | ||
from datahub.sdk.container import Container | ||
from datahub.sdk.dataset import Dataset | ||
from datahub.sdk.main_client import DataHubClient | ||
|
||
warnings.warn( | ||
"The new datahub SDK (e.g. datahub.sdk.*) is experimental. " | ||
"Our typical backwards-compatibility and stability guarantees do not apply to this code. " | ||
"When it's promoted to stable, the import path will change " | ||
"from `from datahub.sdk import ...` to `from datahub import ...`.", | ||
ExperimentalWarning, | ||
stacklevel=2, | ||
) | ||
del warnings | ||
del ExperimentalWarning |
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,15 @@ | ||
from typing import Dict, List, Type | ||
|
||
from datahub.sdk._entity import Entity | ||
from datahub.sdk.container import Container | ||
from datahub.sdk.dataset import Dataset | ||
|
||
# TODO: Is there a better way to declare this? | ||
ENTITY_CLASSES_LIST: List[Type[Entity]] = [ | ||
Container, | ||
Dataset, | ||
] | ||
|
||
ENTITY_CLASSES: Dict[str, Type[Entity]] = { | ||
cls.get_urn_type().ENTITY_TYPE: cls for cls in ENTITY_CLASSES_LIST | ||
} |
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,48 @@ | ||
from __future__ import annotations | ||
|
||
import contextlib | ||
from typing import Iterator | ||
|
||
from datahub.utilities.str_enum import StrEnum | ||
|
||
|
||
class KnownAttribution(StrEnum): | ||
INGESTION = "INGESTION" | ||
INGESTION_ALTERNATE = "INGESTION_ALTERNATE" | ||
|
||
UI = "UI" | ||
SDK = "SDK" | ||
|
||
PROPAGATION = "PROPAGATION" | ||
|
||
def is_ingestion(self) -> bool: | ||
return self in ( | ||
KnownAttribution.INGESTION, | ||
KnownAttribution.INGESTION_ALTERNATE, | ||
) | ||
|
||
|
||
_default_attribution = KnownAttribution.SDK | ||
|
||
|
||
def get_default_attribution() -> KnownAttribution: | ||
return _default_attribution | ||
|
||
|
||
def set_default_attribution(attribution: KnownAttribution) -> None: | ||
global _default_attribution | ||
_default_attribution = attribution | ||
|
||
|
||
@contextlib.contextmanager | ||
def change_default_attribution(attribution: KnownAttribution) -> Iterator[None]: | ||
old_attribution = get_default_attribution() | ||
try: | ||
set_default_attribution(attribution) | ||
yield | ||
finally: | ||
set_default_attribution(old_attribution) | ||
|
||
|
||
def is_ingestion_attribution() -> bool: | ||
return get_default_attribution().is_ingestion() |
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,89 @@ | ||
import abc | ||
from typing import List, Optional, Type, Union | ||
|
||
from typing_extensions import Self | ||
|
||
import datahub.metadata.schema_classes as models | ||
from datahub.emitter.mce_builder import Aspect as AspectTypeVar | ||
from datahub.emitter.mcp import MetadataChangeProposalWrapper | ||
from datahub.errors import SdkUsageError | ||
from datahub.metadata.urns import Urn | ||
from datahub.utilities.urns._urn_base import _SpecificUrn | ||
|
||
|
||
class Entity: | ||
__slots__ = ("_urn", "_prev_aspects", "_aspects") | ||
|
||
def __init__(self, /, urn: Urn): | ||
# This method is not meant for direct usage. | ||
if type(self) is Entity: | ||
raise SdkUsageError(f"{Entity.__name__} cannot be instantiated directly.") | ||
|
||
assert isinstance(urn, self.get_urn_type()) | ||
self._urn: _SpecificUrn = urn | ||
|
||
# prev_aspects is None means this was created from scratch | ||
self._prev_aspects: Optional[models.AspectBag] = None | ||
self._aspects: models.AspectBag = {} | ||
|
||
@classmethod | ||
def _new_from_graph(cls, urn: Urn, current_aspects: models.AspectBag) -> Self: | ||
# If an init method from a subclass adds required fields, it also needs to override this method. | ||
# An alternative approach would call cls.__new__() to bypass the init method, but it's a bit | ||
# too hacky for my taste. | ||
entity = cls(urn=urn) | ||
return entity._init_from_graph(current_aspects) | ||
|
||
def _init_from_graph(self, current_aspects: models.AspectBag) -> Self: | ||
self._prev_aspects = current_aspects | ||
aspect: models._Aspect | ||
for aspect_name, aspect in (current_aspects or {}).items(): # type: ignore | ||
aspect_copy = type(aspect).from_obj(aspect.to_obj()) | ||
self._aspects[aspect_name] = aspect_copy # type: ignore | ||
return self | ||
|
||
@classmethod | ||
@abc.abstractmethod | ||
def get_urn_type(cls) -> Type[_SpecificUrn]: ... | ||
|
||
@property | ||
def urn(self) -> _SpecificUrn: | ||
return self._urn | ||
|
||
def _get_aspect( | ||
self, | ||
aspect_type: Type[AspectTypeVar], | ||
/, | ||
) -> Optional[AspectTypeVar]: | ||
return self._aspects.get(aspect_type.ASPECT_NAME) # type: ignore | ||
|
||
def _set_aspect(self, value: AspectTypeVar, /) -> None: | ||
self._aspects[value.ASPECT_NAME] = value # type: ignore | ||
|
||
def _setdefault_aspect(self, default_aspect: AspectTypeVar, /) -> AspectTypeVar: | ||
# Similar semantics to dict.setdefault. | ||
if existing_aspect := self._get_aspect(type(default_aspect)): | ||
return existing_aspect | ||
self._set_aspect(default_aspect) | ||
return default_aspect | ||
|
||
def _as_mcps( | ||
self, | ||
change_type: Union[str, models.ChangeTypeClass] = models.ChangeTypeClass.UPSERT, | ||
) -> List[MetadataChangeProposalWrapper]: | ||
urn_str = str(self.urn) | ||
|
||
mcps = [] | ||
for aspect in self._aspects.values(): | ||
assert isinstance(aspect, models._Aspect) | ||
mcps.append( | ||
MetadataChangeProposalWrapper( | ||
entityUrn=urn_str, | ||
aspect=aspect, | ||
changeType=change_type, | ||
) | ||
) | ||
return mcps | ||
|
||
def __repr__(self) -> str: | ||
return f"{self.__class__.__name__}('{self.urn}')" |
Oops, something went wrong.
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.
should we consider custom attributions beyond the listed ones here?
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.
yes we will - this is not the final form. attribution will likely be expanded upon a bunch, but this works well enough for an initial experimental impl