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

[pythonic resources][rfc] Adjust EnvVar behavior to explicitly disallow direct usage #14490

Merged
merged 7 commits into from
Sep 30, 2023

Conversation

benpankow
Copy link
Member

@benpankow benpankow commented May 26, 2023

Summary

Alternative RFC to #14488. Keeps EnvVar as a direct subclass of str but errors on resolving the value via __str__ to explicitly prevent users from using EnvVar outside of resource/config contexts. Right now we're sort of in the worst of both worlds where a user can use an EnvVar in inapproporiate places and they get an odd result.

  1. Users who try to use an EnvVar value as a string will hit an error:
my_env_var = EnvVar("MY_ENV_VAR")

# errors!
print(f"Hello, {my_env_var}!")
  1. Users can directly access the EnvVar name or value:
my_env_var = EnvVar("MY_ENV_VAR")

print(f"Env var {my_env_var.env_var_name} has value {my_env_var.get_value()}")

Test Plan

Set of unit tests, existing unit tests.

@benpankow
Copy link
Member Author

benpankow commented May 26, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@schrockn
Copy link
Member

This one seems better to me. We could also offer a method to access the underlying value so that users won't have to import os if they find that annoying.

@benpankow benpankow requested a review from gibsondan September 29, 2023 18:10
@benpankow benpankow force-pushed the benpankow/rfc-late-access-env-var-2 branch from 1b9048d to 1ee1c12 Compare September 29, 2023 18:11
@github-actions
Copy link

github-actions bot commented Sep 29, 2023

Deploy preview for dagit-storybook ready!

✅ Preview
https://dagit-storybook-hnxsdvvfq-elementl.vercel.app
https://benpankow-rfc-late-access-env-var-2.components-storybook.dagster-docs.io

Built with commit cb4aa7c.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link

github-actions bot commented Sep 29, 2023

Deploy preview for dagit-core-storybook ready!

✅ Preview
https://dagit-core-storybook-6f9osqza7-elementl.vercel.app
https://benpankow-rfc-late-access-env-var-2.core-storybook.dagster-docs.io

Built with commit cb4aa7c.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link

Deploy preview for dagster-university ready!

✅ Preview
https://dagster-university-h0jj34y6x-elementl.vercel.app
https://benpankow-rfc-late-access-env-var-2.dagster-university.dagster-docs.io

Built with commit 1ee1c12.
This pull request is being automatically deployed with vercel-action

Comment on lines 498 to 499
f'Attempted to directly retrieve environment variable IntEnvVar("{self.name}").'
" IntEnvVar should only be used as input to Dagster config or resources."
Copy link
Member

Choose a reason for hiding this comment

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

Add:

"If you want to access the environment variable directly, you can call get_value on this object, or os.getenv directly"

Copy link
Member

Choose a reason for hiding this comment

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

We could also save.

"The purpose of EnvVar is to defer retrieval to right before the step is executed, and to be viewable in the UI"

Copy link
Member

@schrockn schrockn left a comment

Choose a reason for hiding this comment

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

Let's do a turn on the error messaging, which is the most important part of this PR.

Comment on lines 537 to 539
f'Attempted to directly retrieve environment variable EnvVar("{self.env_var_name}").'
" EnvVar should only be used as input to Dagster config or resources."
)
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

Comment on lines 498 to 499
f'Attempted to directly retrieve environment variable IntEnvVar("{self.name}").'
" IntEnvVar should only be used as input to Dagster config or resources."
Copy link
Member

Choose a reason for hiding this comment

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

We could also save.

"The purpose of EnvVar is to defer retrieval to right before the step is executed, and to be viewable in the UI"

@benpankow
Copy link
Member Author

Updated copy 👍

@benpankow benpankow requested a review from schrockn September 29, 2023 23:37
Copy link
Member

@schrockn schrockn left a comment

Choose a reason for hiding this comment

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

Great. Consider a common function to share between two callsites to generate error string.

"""
raise RuntimeError(
f'Attempted to directly retrieve environment variable EnvVar("{self.env_var_name}").'
" EnvVar defers resolution of the env var value until run time, and should only be"
Copy link
Member

Choose a reason for hiding this comment

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

spell out "environment variable" instead of "env var"

@benpankow benpankow force-pushed the benpankow/rfc-late-access-env-var-2 branch from ff9101c to cb4aa7c Compare September 30, 2023 18:29
@benpankow benpankow merged commit ee8a150 into master Sep 30, 2023
3 checks passed
@benpankow benpankow deleted the benpankow/rfc-late-access-env-var-2 branch September 30, 2023 19:01
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.

2 participants