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

[docs] fix pyright issues from adding context type hints #17193

Merged
merged 5 commits into from
Nov 10, 2023

Conversation

jamiedemaria
Copy link
Contributor

@jamiedemaria jamiedemaria commented Oct 13, 2023

Summary & Motivation

Adding type hints to the context resulted in several pyright errors. These mostly happened when methods on the context had Optional return types, but we were not handling the None return on the calling side. I did my best to handle these cases in the code snippets to accurately model what users may need to do in their code.

How I Tested These Changes

@jamiedemaria
Copy link
Contributor Author

jamiedemaria commented Oct 13, 2023

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@@ -166,8 +167,8 @@ def image_sensor(context: SensorEvaluationContext):
new_images = [
img_filename
for img_filename in os.listdir(os.getenv("MY_DIRECTORY"))
if not context.instance.has_dynamic_partition(
images_partitions_def.name, img_filename
if not images_partitions_def.has_partition_key(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clairelin135 interested in your opinion if this is an ok pattern to suggest. The issue in the old snippet was that images_partitions_def.name is an Optional[str], but context.instance.has_dynamic_partition is expecting a str. Switching to this method bypasses the optionality issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, this looks reasonable to me

previous_model_accuracy = materialization.asset_materialization.metadata[
"model_accuracy"
]
previous_model_accuracy = None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odette-elementl interested in your sign-off on these changes. Most of it is type casting so that the comparisons are between float types instead of float and MetadataValue which pyright doesn't think is valid

@@ -29,7 +29,7 @@ def get_iris_data_for_date(*args, **kwargs):
},
)
def iris_data_partitioned(context: AssetExecutionContext) -> pd.DataFrame:
partition = partition = context.partition_key.keys_by_dimension
partition = partition = context.partition_key.keys_by_dimension # type: ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont have a better idea for this one. We know the partition_key is going to be a MultiPartitionKey, but the context.partition_key property is annotated to return only a str for backcompat reasons (i think). If we do a condition on isinstance(context.partition_key, MultiPartitionKey) then there's the else case that would return None, which would force this asset to be a optional-return asset, which would be too complicated for this example

@jamiedemaria jamiedemaria marked this pull request as ready for review October 13, 2023 15:24
@jamiedemaria jamiedemaria requested a review from yuhan October 13, 2023 15:24
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints branch from 10ed171 to 9c752b8 Compare October 16, 2023 14:27
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints-pyright branch from adcc725 to dfac25f Compare October 16, 2023 14:27
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints branch from 9c752b8 to 676eb12 Compare October 18, 2023 15:53
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints-pyright branch from dfac25f to 10c16d9 Compare October 18, 2023 15:53
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints branch from 676eb12 to 0ffd8cf Compare October 26, 2023 15:48
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints-pyright branch from 10c16d9 to 0a53756 Compare October 26, 2023 15:48
@jamiedemaria
Copy link
Contributor Author

@yuhan could I get your review on these code snippet changes for the docs?

@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints branch from 0ffd8cf to 0c8b44c Compare November 6, 2023 15:50
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints-pyright branch from 0a53756 to 2d9144e Compare November 6, 2023 15:50
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints branch from 0c8b44c to a958f6b Compare November 8, 2023 17:55
@jamiedemaria jamiedemaria force-pushed the jamie/docs-type-hints-pyright branch from ae3ec0d to d2390d6 Compare November 8, 2023 17:55
@jamiedemaria jamiedemaria merged commit 56598f2 into jamie/docs-type-hints Nov 10, 2023
1 of 2 checks passed
@jamiedemaria jamiedemaria deleted the jamie/docs-type-hints-pyright branch November 10, 2023 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants