Skip to content

Commit

Permalink
[Tidy first] Add some mypy types to parser files (#10380)
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank authored Jul 8, 2024
1 parent a309283 commit 4ea0e10
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 61 deletions.
3 changes: 1 addition & 2 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
ManifestNode,
Metric,
ModelNode,
ResultNode,
SavedQuery,
SeedNode,
SemanticModel,
Expand Down Expand Up @@ -1586,7 +1585,7 @@ def add_disabled_nofile(self, node: GraphMemberNode):
else:
self.disabled[node.unique_id] = [node]

def add_disabled(self, source_file: AnySourceFile, node: ResultNode, test_from=None):
def add_disabled(self, source_file: AnySourceFile, node: GraphMemberNode, test_from=None):
self.add_disabled_nofile(node)
if isinstance(source_file, SchemaSourceFile):
if isinstance(node, GenericTestNode):
Expand Down
12 changes: 6 additions & 6 deletions core/dbt/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dbt.adapters.factory import get_adapter # noqa: F401
from dbt.artifacts.resources import Contract
from dbt.clients.jinja import MacroGenerator, get_rendered
from dbt.config import Project, RuntimeConfig
from dbt.config import RuntimeConfig
from dbt.context.context_config import ContextConfig
from dbt.context.providers import (
generate_generate_name_macro_context,
Expand Down Expand Up @@ -39,9 +39,9 @@


class BaseParser(Generic[FinalValue]):
def __init__(self, project: Project, manifest: Manifest) -> None:
self.project = project
self.manifest = manifest
def __init__(self, project: RuntimeConfig, manifest: Manifest) -> None:
self.project: RuntimeConfig = project
self.manifest: Manifest = manifest

@abc.abstractmethod
def parse_file(self, block: FileBlock) -> None:
Expand All @@ -63,7 +63,7 @@ def generate_unique_id(self, resource_name: str, hash: Optional[str] = None) ->
class Parser(BaseParser[FinalValue], Generic[FinalValue]):
def __init__(
self,
project: Project,
project: RuntimeConfig,
manifest: Manifest,
root_project: RuntimeConfig,
) -> None:
Expand Down Expand Up @@ -121,7 +121,7 @@ class ConfiguredParser(
):
def __init__(
self,
project: Project,
project: RuntimeConfig,
manifest: Manifest,
root_project: RuntimeConfig,
) -> None:
Expand Down
6 changes: 3 additions & 3 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ class ManifestLoader:
def __init__(
self,
root_project: RuntimeConfig,
all_projects: Mapping[str, Project],
all_projects: Mapping[str, RuntimeConfig],
macro_hook: Optional[Callable[[Manifest], Any]] = None,
file_diff: Optional[FileDiff] = None,
) -> None:
self.root_project: RuntimeConfig = root_project
self.all_projects: Mapping[str, Project] = all_projects
self.all_projects: Mapping[str, RuntimeConfig] = all_projects
self.file_diff = file_diff
self.manifest: Manifest = Manifest()
self.new_manifest = self.manifest
Expand Down Expand Up @@ -669,7 +669,7 @@ def load_and_parse_macros(self, project_parser_files):
# 'parser_types'
def parse_project(
self,
project: Project,
project: RuntimeConfig,
parser_files,
parser_types: List[Type[Parser]],
) -> None:
Expand Down
20 changes: 13 additions & 7 deletions core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
generate_parse_exposure,
generate_parse_semantic_models,
)
from dbt.contracts.files import SchemaSourceFile
from dbt.contracts.graph.nodes import Exposure, Group, Metric, SavedQuery, SemanticModel
from dbt.contracts.graph.unparsed import (
UnparsedConversionTypeParams,
Expand Down Expand Up @@ -85,7 +86,7 @@ def __init__(self, schema_parser: SchemaParser, yaml: YamlBlock) -> None:
self.schema_parser = schema_parser
self.yaml = yaml

def parse_exposure(self, unparsed: UnparsedExposure):
def parse_exposure(self, unparsed: UnparsedExposure) -> None:
package_name = self.project.project_name
unique_id = f"{NodeType.Exposure}.{package_name}.{unparsed.name}"
path = self.yaml.path.relative_path
Expand Down Expand Up @@ -143,6 +144,7 @@ def parse_exposure(self, unparsed: UnparsedExposure):
get_rendered(depends_on_jinja, ctx, parsed, capture_macros=True)
# parsed now has a populated refs/sources/metrics

assert isinstance(self.yaml.file, SchemaSourceFile)
if parsed.config.enabled:
self.manifest.add_exposure(self.yaml.file, parsed)
else:
Expand Down Expand Up @@ -171,7 +173,7 @@ def _generate_exposure_config(
patch_config_dict=precedence_configs,
)

def parse(self):
def parse(self) -> None:
for data in self.get_key_dicts():
try:
UnparsedExposure.validate(data)
Expand Down Expand Up @@ -387,7 +389,7 @@ def _get_metric_type_params(self, unparsed_metric: UnparsedMetric) -> MetricType
# input_measures=?,
)

def parse_metric(self, unparsed: UnparsedMetric, generated: bool = False):
def parse_metric(self, unparsed: UnparsedMetric, generated: bool = False) -> None:
package_name = self.project.project_name
unique_id = f"{NodeType.Metric}.{package_name}.{unparsed.name}"
path = self.yaml.path.relative_path
Expand Down Expand Up @@ -442,6 +444,7 @@ def parse_metric(self, unparsed: UnparsedMetric, generated: bool = False):
)

# if the metric is disabled we do not want it included in the manifest, only in the disabled dict
assert isinstance(self.yaml.file, SchemaSourceFile)
if parsed.config.enabled:
self.manifest.add_metric(self.yaml.file, parsed, generated)
else:
Expand Down Expand Up @@ -471,7 +474,7 @@ def _generate_metric_config(
)
return config

def parse(self):
def parse(self) -> None:
for data in self.get_key_dicts():
try:
UnparsedMetric.validate(data)
Expand All @@ -488,7 +491,7 @@ def __init__(self, schema_parser: SchemaParser, yaml: YamlBlock) -> None:
self.schema_parser = schema_parser
self.yaml = yaml

def parse_group(self, unparsed: UnparsedGroup):
def parse_group(self, unparsed: UnparsedGroup) -> None:
package_name = self.project.project_name
unique_id = f"{NodeType.Group}.{package_name}.{unparsed.name}"
path = self.yaml.path.relative_path
Expand All @@ -503,6 +506,7 @@ def parse_group(self, unparsed: UnparsedGroup):
owner=unparsed.owner,
)

assert isinstance(self.yaml.file, SchemaSourceFile)
self.manifest.add_group(self.yaml.file, parsed)

def parse(self):
Expand Down Expand Up @@ -635,7 +639,7 @@ def _generate_semantic_model_config(

return config

def parse_semantic_model(self, unparsed: UnparsedSemanticModel):
def parse_semantic_model(self, unparsed: UnparsedSemanticModel) -> None:
package_name = self.project.project_name
unique_id = f"{NodeType.SemanticModel}.{package_name}.{unparsed.name}"
path = self.yaml.path.relative_path
Expand Down Expand Up @@ -695,6 +699,7 @@ def parse_semantic_model(self, unparsed: UnparsedSemanticModel):

# if the semantic model is disabled we do not want it included in the manifest,
# only in the disabled dict
assert isinstance(self.yaml.file, SchemaSourceFile)
if parsed.config.enabled:
self.manifest.add_semantic_model(self.yaml.file, parsed)
else:
Expand All @@ -705,7 +710,7 @@ def parse_semantic_model(self, unparsed: UnparsedSemanticModel):
if measure.create_metric is True:
self._create_metric(measure=measure, enabled=parsed.config.enabled)

def parse(self):
def parse(self) -> None:
for data in self.get_key_dicts():
try:
UnparsedSemanticModel.validate(data)
Expand Down Expand Up @@ -831,6 +836,7 @@ def parse_saved_query(self, unparsed: UnparsedSavedQuery) -> None:
delattr(export, "relation_name")

# Only add thes saved query if it's enabled, otherwise we track it with other diabled nodes
assert isinstance(self.yaml.file, SchemaSourceFile)
if parsed.config.enabled:
self.manifest.add_saved_query(self.yaml.file, parsed)
else:
Expand Down
Loading

0 comments on commit 4ea0e10

Please sign in to comment.