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

Make Jinja2Templates.get_env private & rename #1218

Merged
merged 2 commits into from Jun 27, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions starlette/templating.py
Expand Up @@ -56,9 +56,9 @@ class Jinja2Templates:

def __init__(self, directory: str) -> None:
assert jinja2 is not None, "jinja2 must be installed to use Jinja2Templates"
self.env = self.get_env(directory)
self.env = self._get_env(directory)

def get_env(self, directory: str) -> "jinja2.Environment":
def _get_env(self, directory: str) -> "jinja2.Environment":
Copy link
Sponsor Member

@Kludex Kludex Jun 26, 2021

Choose a reason for hiding this comment

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

@JayH5 I've been thinking about this kind of things:

Suggested change
def _get_env(self, directory: str) -> "jinja2.Environment":
def get_env(self, directory: str) -> "jinja2.Environment":
warnings.warn(
"'get_env' is deprecated. Use '_get_env' instead.",
DeprecationWarning,
)
return self._get_env(directory)
def _get_env(self, directory: str) -> "jinja2.Environment":

We did it for GraphQL, but we are in a pre-stable version, so in theory we can be aggressive and remove without adding warnings 🤔

Otherwise, we should be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the whole idea is to shy away from using get_env direcrly. We can change the warning to encourage using templates.env instead of templates.get_env()

Copy link
Member

Choose a reason for hiding this comment

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

We definitely wouldn't want to suggest using a private method. We could say [...] Use the env attribute instead but even that is not equivalent to get_env.

If we wanted to be really careful we could rename this method to create_env and then call it from get_env with a warning like "this is deprecated, you probably want the env attribute, but if not use create_env". But, IMO, people should basically never want what get_env currently does and it's only confusing. I agree it should be removed.

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

We definitely wouldn't want to suggest using a private method.

Yeah, I agree! I've copied the warning from another snippet. 😛

But my intention here was to bring the discussion if we should keep adding warnings in pre-1.0.

@pass_context
def url_for(context: dict, name: str, **path_params: typing.Any) -> str:
request = context["request"]
Expand Down