Skip to content

Conversation

@jonathan-conder
Copy link
Contributor

The directory isn't guaranteed to exist after the context manager exits.


  • Have you followed the guidelines for contributing?
  • Have you signed the CLA?
  • Have you successfully run make lint && make test?
  • Have you added an entry to the changelog (docs/reference/changelog.rst)?

There were some unrelated lint/test failures.

@jonathan-conder jonathan-conder requested review from a team and medubelko as code owners June 30, 2025 06:39
Copy link
Member

@bepri bepri left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for updating the tests too. Just one nit :)


@property
def parent_template_dir(self) -> pathlib.Path:
def parent_template_dir(self) -> AbstractContextManager[pathlib.Path]:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def parent_template_dir(self) -> AbstractContextManager[pathlib.Path]:
@contextlib.contextmanager
def parent_template_dir(self) -> pathlib.Path
yield importlib.resources.path(self._app.name, "templates")

It's not letting me highlight the whole block for this suggestion (grr) so sorry if it looks weird. I would wrap this using a decorator instead, it's a more common pattern with our tools.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Under this example wouldn't it need to be something like:

with importlib.resources.path(...) as p:
    yield p

Otherwise you'd have a context manager that yields a context manager.

Copy link
Member

Choose a reason for hiding this comment

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

facepalm, yeah, that's correct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The return type isn't right, do you have any preferences between Iterator[pathlib.Path], Generator[pathlib.Path] or Iterable[pathlib.Path]?

I'll probably revisit this next week to add the back-compatibility.

Copy link
Member

Choose a reason for hiding this comment

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

I think Generator makes the most sense to me for something that's meant to be used as a context manager, but I don't have a strong preference

@bepri bepri requested a review from a team July 2, 2025 17:36

@property
def parent_template_dir(self) -> pathlib.Path:
def parent_template_dir(self) -> AbstractContextManager[pathlib.Path]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a breaking change, since a child class could override this. We might need to instead deprecate this method and add a new one with the right behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I've attempted it. Probably not 100% compatible since the subclass could override __getattribute__ or something weird like that.

Copy link
Contributor

@medubelko medubelko left a comment

Choose a reason for hiding this comment

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

Just a small suggestion for the changelog.

@jonathan-conder jonathan-conder force-pushed the fix/parent-template-dir branch 2 times, most recently from 1b850e6 to 265bd1f Compare July 27, 2025 21:07
@jonathan-conder jonathan-conder force-pushed the fix/parent-template-dir branch from 265bd1f to a724ea6 Compare November 3, 2025 10:16
The directory isn't guaranteed to exist after the context manager exits.
@jonathan-conder jonathan-conder force-pushed the fix/parent-template-dir branch from a724ea6 to 8d6abd3 Compare November 3, 2025 10:21
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.

4 participants