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

Use isinstance() over hasattr() for type guard #27211

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from collections.abc import Sequence
from typing import TYPE_CHECKING, Union

from typing_extensions import TypeIs

import dagster._check as check
from dagster._annotations import public
from dagster._core.definitions.asset_key import T_EntityKey
Expand All @@ -11,13 +13,43 @@
BuiltinAutomationCondition,
)
from dagster._core.definitions.declarative_automation.automation_context import AutomationContext
from dagster._core.definitions.declarative_automation.operators.dep_operators import (
DepsAutomationCondition,
)
from dagster._record import copy, record
from dagster._serdes.serdes import whitelist_for_serdes

if TYPE_CHECKING:
from dagster._core.definitions.asset_selection import AssetSelection


def _has_allow_ignore(
condition: AutomationCondition,
) -> TypeIs[
Union[
DepsAutomationCondition,
"AndAutomationCondition",
"OrAutomationCondition",
"NotAutomationCondition",
]
]:
from dagster._core.definitions.declarative_automation.operators.boolean_operators import (
AndAutomationCondition,
NotAutomationCondition,
OrAutomationCondition,
)

return isinstance(
condition,
(
DepsAutomationCondition,
AndAutomationCondition,
OrAutomationCondition,
NotAutomationCondition,
),
)


@whitelist_for_serdes(storage_name="AndAssetCondition")
@record
class AndAutomationCondition(BuiltinAutomationCondition[T_EntityKey]):
Expand Down Expand Up @@ -98,7 +130,7 @@ def allow(self, selection: "AssetSelection") -> "AndAutomationCondition":
return copy(
self,
operands=[
child.allow(selection) if hasattr(child, "allow") else child
child.allow(selection) if _has_allow_ignore(child) else child
for child in self.operands
],
)
Expand All @@ -118,7 +150,7 @@ def ignore(self, selection: "AssetSelection") -> "AndAutomationCondition":
return copy(
self,
operands=[
child.ignore(selection) if hasattr(child, "ignore") else child
child.ignore(selection) if _has_allow_ignore(child) else child
for child in self.operands
],
)
Expand Down Expand Up @@ -199,7 +231,7 @@ def allow(self, selection: "AssetSelection") -> "OrAutomationCondition":
return copy(
self,
operands=[
child.allow(selection) if hasattr(child, "allow") else child
child.allow(selection) if _has_allow_ignore(child) else child
for child in self.operands
],
)
Expand All @@ -219,7 +251,7 @@ def ignore(self, selection: "AssetSelection") -> "OrAutomationCondition":
return copy(
self,
operands=[
child.ignore(selection) if hasattr(child, "ignore") else child
child.ignore(selection) if _has_allow_ignore(child) else child
for child in self.operands
],
)
Expand Down Expand Up @@ -288,7 +320,7 @@ def allow(self, selection: "AssetSelection") -> "NotAutomationCondition":
return copy(
self,
operand=self.operand.allow(selection)
if hasattr(self.operand, "allow")
if _has_allow_ignore(self.operand)
else self.operand,
)

Expand All @@ -307,6 +339,6 @@ def ignore(self, selection: "AssetSelection") -> "NotAutomationCondition":
return copy(
self,
operand=self.operand.ignore(selection)
if hasattr(self.operand, "ignore")
if _has_allow_ignore(self.operand)
else self.operand,
)
2 changes: 1 addition & 1 deletion python_modules/dagster/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_version() -> str:
"tabulate",
"tomli<3",
"tqdm<5",
"typing_extensions>=4.4.0,<5",
"typing_extensions>=4.10.0,<5",
'tzdata; platform_system=="Windows"',
"structlog",
"sqlalchemy>=1.0,<3",
Expand Down
Loading