Skip to content

Commit

Permalink
mypy is rightfully mad about our funky inheritance, just copy+paste t…
Browse files Browse the repository at this point in the history
…hings

We can fix this when we drop python 3.6 and unions stop collapsing types
  • Loading branch information
Jacob Beck committed Aug 11, 2020
1 parent 2a4fe40 commit d77113d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 75 deletions.
28 changes: 22 additions & 6 deletions core/dbt/contracts/graph/compiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
ParsedSeedNode,
ParsedSnapshotNode,
ParsedSourceDefinition,
SchemaTestMixin,
SeedMixin,
SeedConfig,
TestConfig,
compare_seeds,
)
from dbt.node_types import NodeType
from dbt.contracts.util import Replaceable
Expand Down Expand Up @@ -94,8 +94,18 @@ class CompiledRPCNode(CompiledNode):


@dataclass
class CompiledSeedNode(SeedMixin, CompiledNode):
pass
class CompiledSeedNode(CompiledNode):
# keep this in sync with ParsedSeedNode!
resource_type: NodeType = field(metadata={'restrict': [NodeType.Seed]})
config: SeedConfig = field(default_factory=SeedConfig)

@property
def empty(self):
""" Seeds are never empty"""
return False

def _same_body(self, other) -> bool:
return compare_seeds(self, other)


@dataclass
Expand All @@ -110,8 +120,14 @@ class CompiledDataTestNode(CompiledNode):


@dataclass
class CompiledSchemaTestNode(SchemaTestMixin, CompiledNode, HasTestMetadata):
pass
class CompiledSchemaTestNode(CompiledNode, HasTestMetadata):
# keep this in sync with ParsedSchemaTestNode!
resource_type: NodeType = field(metadata={'restrict': [NodeType.Test]})
column_name: Optional[str] = None
config: TestConfig = field(default_factory=TestConfig)

def _same_body(self, other) -> bool:
return self.test_metadata == other.test_metadata


CompiledTestNode = Union[CompiledDataTestNode, CompiledSchemaTestNode]
Expand Down
120 changes: 54 additions & 66 deletions core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,66 +249,13 @@ class ParsedAnalysisNode(ParsedNode):


@dataclass
class HookMixin(JsonSchemaMixin):
class ParsedHookNode(ParsedNode):
resource_type: NodeType = field(
metadata={'restrict': [NodeType.Operation]}
)
index: Optional[int] = None


@dataclass
class SeedMixin(JsonSchemaMixin):
resource_type: NodeType = field(metadata={'restrict': [NodeType.Seed]})
config: SeedConfig = field(default_factory=SeedConfig)

@property
def empty(self):
""" Seeds are never empty"""
return False

def _same_body(self: 'ParsedSeedNode', other: 'ParsedSeedNode') -> bool:
# for seeds, we check the hashes. If the hashes are different types,
# no match. If the hashes are both the same 'path', log a warning and
# assume they are the same
# if the current checksum is a path, we want to log a warning.
result = self.checksum == other.checksum

if self.checksum.name == 'path':
msg: str
if other.checksum.name != 'path':
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size. The '
f'previous file was <={MAXIMUM_SEED_SIZE_NAME}, so it '
f'has changed'
)
elif result:
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size at '
f'the same path, dbt cannot tell if it has changed: '
f'assuming they are the same'
)
elif not result:
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size. The '
f'previous file was in a different location, assuming it '
f'has changed'
)
else:
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size. The '
f'previous file had a checksum type of '
f'{other.checksum.name}, so it has changed'
)
warn_or_error(msg, node=self)

return result


@dataclass
class ParsedHookNode(HookMixin, ParsedNode):
pass


@dataclass
class ParsedModelNode(ParsedNode):
resource_type: NodeType = field(metadata={'restrict': [NodeType.Model]})
Expand All @@ -319,9 +266,57 @@ class ParsedRPCNode(ParsedNode):
resource_type: NodeType = field(metadata={'restrict': [NodeType.RPCCall]})


def compare_seeds(first: ParsedNode, second: ParsedNode) -> bool:
# for seeds, we check the hashes. If the hashes are different types,
# no match. If the hashes are both the same 'path', log a warning and
# assume they are the same
# if the current checksum is a path, we want to log a warning.
result = first.checksum == second.checksum

if first.checksum.name == 'path':
msg: str
if second.checksum.name != 'path':
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size. The '
f'previous file was <={MAXIMUM_SEED_SIZE_NAME}, so it '
f'has changed'
)
elif result:
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size at '
f'the same path, dbt cannot tell if it has changed: '
f'assuming they are the same'
)
elif not result:
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size. The '
f'previous file was in a different location, assuming it '
f'has changed'
)
else:
msg = (
f'Found a seed >{MAXIMUM_SEED_SIZE_NAME} in size. The '
f'previous file had a checksum type of '
f'{second.checksum.name}, so it has changed'
)
warn_or_error(msg, node=first)

return result


@dataclass
class ParsedSeedNode(SeedMixin, ParsedNode):
pass
class ParsedSeedNode(ParsedNode):
# keep this in sync with CompiledSeedNode!
resource_type: NodeType = field(metadata={'restrict': [NodeType.Seed]})
config: SeedConfig = field(default_factory=SeedConfig)

@property
def empty(self):
""" Seeds are never empty"""
return False

def _same_body(self: T, other: T) -> bool:
return compare_seeds(self, other)


@dataclass
Expand All @@ -343,23 +338,16 @@ class ParsedDataTestNode(ParsedNode):


@dataclass
class SchemaTestMixin(JsonSchemaMixin):
class ParsedSchemaTestNode(ParsedNode, HasTestMetadata):
# keep this in sync with CompiledSchemaTestNode!
resource_type: NodeType = field(metadata={'restrict': [NodeType.Test]})
column_name: Optional[str] = None
config: TestConfig = field(default_factory=TestConfig)

# make sure to keep this in sync with CompiledSchemaTestNode...
def _same_body(
self: 'ParsedSchemaTestNode', other: 'ParsedSchemaTestNode'
) -> bool:
def _same_body(self: HasTestMetadata, other: HasTestMetadata) -> bool:
return self.test_metadata == other.test_metadata


@dataclass
class ParsedSchemaTestNode(SchemaTestMixin, ParsedNode, HasTestMetadata):
pass


@dataclass
class IntermediateSnapshotNode(ParsedNode):
# at an intermediate stage in parsing, where we've built something better
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/graph/selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def check_modified(
'macros. This will not be marked as a modification.'
))

return not new.same_contents(old)
return not new.same_contents(old) # type: ignore

def check_new(
self,
Expand Down
6 changes: 4 additions & 2 deletions core/dbt/parser/seeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def render_with_context(
) -> None:
"""Seeds don't need to do any rendering."""

def load_file(self, match: FilePath) -> SourceFile:
def load_file(
self, match: FilePath, *, set_contents: bool = False
) -> SourceFile:
if match.seed_too_large():
# We don't want to calculate a hash of this file. Use the path.
return SourceFile.big_seed(match)
else:
# We want to calculate a hash, but we don't need the contents
return super().load_file(match, set_contents=False)
return super().load_file(match, set_contents=set_contents)

0 comments on commit d77113d

Please sign in to comment.