-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
exception cleanup #6347
Merged
Merged
exception cleanup #6347
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3d1d938
starting to move jinja exceptions
emmyoop 8797c5d
convert some exceptions
emmyoop 023baef
add back old functions for backward compatibility
emmyoop 0971e32
organize
emmyoop c02fc01
more conversions
emmyoop 493c997
more conversions
emmyoop 9f1d65c
add changelog
emmyoop 0c5d720
split out CacheInconsistency
emmyoop 685d58a
more conversions
emmyoop 9478bb3
convert even more
emmyoop 1037aeb
convert parsingexceptions
emmyoop 4b56659
fix tests
emmyoop 3a739b8
more conversions
emmyoop 72a0072
more conversions
emmyoop d98f4f3
finish converting exception functions
emmyoop 62f8b42
convert more tests
emmyoop d397c38
standardize to msg
emmyoop 1925c11
remove some TODOs
emmyoop c5f9d30
fix test param and check the rest
emmyoop c0406c8
add comment, move exceptions
emmyoop b3bf0be
add types
emmyoop e85fb67
fix type errors
emmyoop 5b86c8b
fix type for adapter_response
emmyoop a6dbe51
remove 0.13 version from message
emmyoop 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
kind: Breaking Changes | ||
body: Cleaned up exceptions to directly raise in code. Removed use of all exception | ||
functions in the code base and marked them all as deprecated to be removed next | ||
minor release. | ||
time: 2022-12-05T14:19:37.863032-06:00 | ||
custom: | ||
Author: emmyoop | ||
Issue: "6339" | ||
PR: "6347" |
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 |
---|---|---|
|
@@ -22,13 +22,20 @@ | |
import pytz | ||
|
||
from dbt.exceptions import ( | ||
raise_database_error, | ||
raise_compiler_error, | ||
invalid_type_error, | ||
get_relation_returned_multiple_results, | ||
InternalException, | ||
InvalidMacroArgType, | ||
InvalidMacroResult, | ||
InvalidQuoteConfigType, | ||
NotImplementedException, | ||
NullRelationCacheAttempted, | ||
NullRelationDropAttempted, | ||
RelationReturnedMultipleResults, | ||
RenameToNoneAttempted, | ||
RuntimeException, | ||
SnapshotTargetIncomplete, | ||
SnapshotTargetNotSnapshotTable, | ||
UnexpectedNull, | ||
UnexpectedNonTimestamp, | ||
) | ||
|
||
from dbt.adapters.protocol import ( | ||
|
@@ -97,18 +104,10 @@ def _utc(dt: Optional[datetime], source: BaseRelation, field_name: str) -> datet | |
assume the datetime is already for UTC and add the timezone. | ||
""" | ||
if dt is None: | ||
raise raise_database_error( | ||
"Expected a non-null value when querying field '{}' of table " | ||
" {} but received value 'null' instead".format(field_name, source) | ||
) | ||
raise UnexpectedNull(field_name, source) | ||
|
||
elif not hasattr(dt, "tzinfo"): | ||
raise raise_database_error( | ||
"Expected a timestamp value when querying field '{}' of table " | ||
"{} but received value of type '{}' instead".format( | ||
field_name, source, type(dt).__name__ | ||
) | ||
) | ||
raise UnexpectedNonTimestamp(field_name, source, dt) | ||
|
||
elif dt.tzinfo: | ||
return dt.astimezone(pytz.UTC) | ||
|
@@ -434,7 +433,7 @@ def cache_added(self, relation: Optional[BaseRelation]) -> str: | |
"""Cache a new relation in dbt. It will show up in `list relations`.""" | ||
if relation is None: | ||
name = self.nice_connection_name() | ||
raise_compiler_error("Attempted to cache a null relation for {}".format(name)) | ||
raise NullRelationCacheAttempted(name) | ||
self.cache.add(relation) | ||
# so jinja doesn't render things | ||
return "" | ||
|
@@ -446,7 +445,7 @@ def cache_dropped(self, relation: Optional[BaseRelation]) -> str: | |
""" | ||
if relation is None: | ||
name = self.nice_connection_name() | ||
raise_compiler_error("Attempted to drop a null relation for {}".format(name)) | ||
raise NullRelationDropAttempted(name) | ||
self.cache.drop(relation) | ||
return "" | ||
|
||
|
@@ -463,9 +462,7 @@ def cache_renamed( | |
name = self.nice_connection_name() | ||
src_name = _relation_name(from_relation) | ||
dst_name = _relation_name(to_relation) | ||
raise_compiler_error( | ||
"Attempted to rename {} to {} for {}".format(src_name, dst_name, name) | ||
) | ||
raise RenameToNoneAttempted(src_name, dst_name, name) | ||
|
||
self.cache.rename(from_relation, to_relation) | ||
return "" | ||
|
@@ -615,19 +612,21 @@ def get_missing_columns( | |
to_relation. | ||
""" | ||
if not isinstance(from_relation, self.Relation): | ||
invalid_type_error( | ||
raise InvalidMacroArgType( | ||
method_name="get_missing_columns", | ||
arg_name="from_relation", | ||
got_value=from_relation, | ||
expected_type=self.Relation, | ||
version="0.13.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of the "version" piece entirely, including in the message? We haven't supported version 0.13.0 in forever, so just a "this isn't supported" should be fine. |
||
) | ||
|
||
if not isinstance(to_relation, self.Relation): | ||
invalid_type_error( | ||
raise InvalidMacroArgType( | ||
method_name="get_missing_columns", | ||
arg_name="to_relation", | ||
got_value=to_relation, | ||
expected_type=self.Relation, | ||
version="0.13.0", | ||
) | ||
|
||
from_columns = {col.name: col for col in self.get_columns_in_relation(from_relation)} | ||
|
@@ -648,11 +647,12 @@ def valid_snapshot_target(self, relation: BaseRelation) -> None: | |
incorrect. | ||
""" | ||
if not isinstance(relation, self.Relation): | ||
invalid_type_error( | ||
raise InvalidMacroArgType( | ||
method_name="valid_snapshot_target", | ||
arg_name="relation", | ||
got_value=relation, | ||
expected_type=self.Relation, | ||
version="0.13.0", | ||
) | ||
|
||
columns = self.get_columns_in_relation(relation) | ||
|
@@ -669,36 +669,30 @@ def valid_snapshot_target(self, relation: BaseRelation) -> None: | |
|
||
if missing: | ||
if extra: | ||
msg = ( | ||
'Snapshot target has ("{}") but not ("{}") - is it an ' | ||
"unmigrated previous version archive?".format( | ||
'", "'.join(extra), '", "'.join(missing) | ||
) | ||
) | ||
raise SnapshotTargetIncomplete(extra, missing) | ||
else: | ||
msg = 'Snapshot target is not a snapshot table (missing "{}")'.format( | ||
'", "'.join(missing) | ||
) | ||
raise_compiler_error(msg) | ||
raise SnapshotTargetNotSnapshotTable(missing) | ||
|
||
@available.parse_none | ||
def expand_target_column_types( | ||
self, from_relation: BaseRelation, to_relation: BaseRelation | ||
) -> None: | ||
if not isinstance(from_relation, self.Relation): | ||
invalid_type_error( | ||
raise InvalidMacroArgType( | ||
method_name="expand_target_column_types", | ||
arg_name="from_relation", | ||
got_value=from_relation, | ||
expected_type=self.Relation, | ||
version="0.13.0", | ||
) | ||
|
||
if not isinstance(to_relation, self.Relation): | ||
invalid_type_error( | ||
raise InvalidMacroArgType( | ||
method_name="expand_target_column_types", | ||
arg_name="to_relation", | ||
got_value=to_relation, | ||
expected_type=self.Relation, | ||
version="0.13.0", | ||
) | ||
|
||
self.expand_column_types(from_relation, to_relation) | ||
|
@@ -776,7 +770,7 @@ def get_relation(self, database: str, schema: str, identifier: str) -> Optional[ | |
"schema": schema, | ||
"database": database, | ||
} | ||
get_relation_returned_multiple_results(kwargs, matches) | ||
raise RelationReturnedMultipleResults(kwargs, matches) | ||
|
||
elif matches: | ||
return matches[0] | ||
|
@@ -840,10 +834,7 @@ def quote_seed_column(self, column: str, quote_config: Optional[bool]) -> str: | |
elif quote_config is None: | ||
pass | ||
else: | ||
raise_compiler_error( | ||
f'The seed configuration value of "quote_columns" has an ' | ||
f"invalid type {type(quote_config)}" | ||
) | ||
raise InvalidQuoteConfigType(quote_config) | ||
|
||
if quote_columns: | ||
return self.quote(column) | ||
|
@@ -1093,11 +1084,7 @@ def calculate_freshness( | |
# now we have a 1-row table of the maximum `loaded_at_field` value and | ||
# the current time according to the db. | ||
if len(table) != 1 or len(table[0]) != 2: | ||
raise_compiler_error( | ||
'Got an invalid result from "{}" macro: {}'.format( | ||
FRESHNESS_MACRO_NAME, [tuple(r) for r in table] | ||
) | ||
) | ||
raise InvalidMacroResult(FRESHNESS_MACRO_NAME, table) | ||
if table[0][0] is None: | ||
# no records in the table, so really the max_loaded_at was | ||
# infinitely long ago. Just call it 0:00 January 1 year UTC | ||
|
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
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
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.
Maybe we've hit the tipping point here where we should just import the dbt.exceptions module?
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.
Is there a reason to do that other than not importing them individually?
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.
I think some Python codebases have policies around that but we don't. I guess if the import list gets really long it might be easier to just import the package. If we did import dbt..exceptions as exc we could specify every one as exc.SomeException. I think some people prefer the specific imports because it makes it clearer which ones are used in this file. I'm kind of agnostic on the whole thing.
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.
I'm in this camp for sure.