Repository navigation
refactor: deprecate Actor.get_env() #1213
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import asyncio | ||
| import os | ||
|
|
||
| from apify import Actor, ApifyEnvVars | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| async with Actor: | ||
| config = Actor.configuration | ||
|
|
||
| Actor.log.info(f'Actor ID: {config.actor_id}') | ||
| Actor.log.info(f'Run ID: {config.actor_run_id}') | ||
| Actor.log.info(f'Default dataset ID: {config.default_dataset_id}') | ||
| Actor.log.info(f'Default KVS ID: {config.default_key_value_store_id}') | ||
|
|
||
| # Variables the configuration does not expose are read from the environment. | ||
| latest_sdk_version = os.environ.get(ApifyEnvVars.SDK_LATEST_VERSION) | ||
| Actor.log.info(f'Latest SDK version: {latest_sdk_version}') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| asyncio.run(main()) |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -981,10 +981,20 @@ def is_at_home(self) -> bool: | |||||||
| def get_env(self) -> dict: | ||||||||
| """Return a dictionary with information parsed from all the `APIFY_XXX` environment variables. | ||||||||
|
|
||||||||
| Deprecated. Read `Actor.configuration` instead, or `os.environ` for variables it does not expose. | ||||||||
| Will be removed in version 5.0.0. | ||||||||
|
|
||||||||
| For a list of all the environment variables, see the | ||||||||
| [Actor documentation](https://docs.apify.com/actors/development/environment-variables). If some variables | ||||||||
| are not defined or are invalid, the corresponding value in the resulting dictionary will be None. | ||||||||
| """ | ||||||||
| warnings.warn( | ||||||||
| '`Actor.get_env()` is deprecated and will be removed in version 5.0.0. Read `Actor.configuration` ' | ||||||||
| 'instead, or `os.environ` for variables it does not expose.', | ||||||||
| DeprecationWarning, | ||||||||
|
Contributor
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. Even with the right stacklevel, |
||||||||
| stacklevel=2, | ||||||||
|
Contributor
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. Important:
Suggested change
|
||||||||
| ) | ||||||||
|
|
||||||||
|
Contributor
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. Outside this diff: the |
||||||||
| config = dict[str, Any]() | ||||||||
| for field_name, field in Configuration.model_fields.items(): | ||||||||
| if field.deprecated: | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||||||||||||||||
| from decimal import Decimal | ||||||||||||||||||||||||
| from typing import TYPE_CHECKING, Any | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||
| from pydantic_core import TzInfo | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| from apify import Actor | ||||||||||||||||||||||||
|
|
@@ -82,8 +83,6 @@ | |||||||||||||||||||||||
| if TYPE_CHECKING: | ||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| async def test_actor_is_not_at_home_when_local() -> None: | ||||||||||||||||||||||||
| async with Actor as actor: | ||||||||||||||||||||||||
|
|
@@ -199,6 +198,15 @@ async def test_get_env_with_randomized_env_vars(monkeypatch: pytest.MonkeyPatch, | |||||||||||||||||||||||
| expected_get_env[ApifyEnvVars.CHARGED_ACTOR_EVENT_COUNTS.name.lower()] = None | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| await Actor.init() | ||||||||||||||||||||||||
| assert Actor.get_env() == expected_get_env | ||||||||||||||||||||||||
| with pytest.warns(DeprecationWarning, match=r'`Actor.get_env\(\)` is deprecated'): | ||||||||||||||||||||||||
| env = Actor.get_env() | ||||||||||||||||||||||||
| assert env == expected_get_env | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| await Actor.exit() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| async def test_get_env_warns_about_deprecation() -> None: | ||||||||||||||||||||||||
| """`Actor.get_env()` emits a `DeprecationWarning` pointing at `Actor.configuration`.""" | ||||||||||||||||||||||||
| async with Actor: | ||||||||||||||||||||||||
| with pytest.warns(DeprecationWarning, match=r'`Actor.get_env\(\)` is deprecated.*`Actor.configuration`'): | ||||||||||||||||||||||||
| Actor.get_env() | ||||||||||||||||||||||||
|
Comment on lines
+209
to
+212
Contributor
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. Suggestion:
Suggested change
|
||||||||||||||||||||||||
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.
Nit: not every unset variable is
None. For example,input_keydefaults to'INPUT'andis_at_hometoFalse.