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

Multiple watch args, future annotations, pytest asserts, and some other patches #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SamSokolin
Copy link

@SamSokolin SamSokolin commented Mar 27, 2024

This PR includes the following changes:

  • Adds support for passing in multiple glob filters to the --watch arg
  • Adds support for future compiler flags (also included by my coworker in this PR: respect future feature flags #37)
  • Adds support for optional patching asserts with pytest asserts for a better DX when using jurigged in a pytest context
  • Pulls in a fix for an off by one error called out by @JamesHutchison here: Pytest hot reloader issues #29

I'm happy to split these out into separate PRs or to refactor as necessary! Ideally we can find the root cause of the off by one error in the underlying codetools logic in jurigged rather than patching it retroactively.

@SamSokolin SamSokolin changed the title adding support for multiple watch args Multiple watch args, future annotations, pytest asserts, and some other patches Apr 12, 2024
Copy link

@JamesHutchison JamesHutchison left a comment

Choose a reason for hiding this comment

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

This is great! Super excited to see things get updated! I'll try this out on https://github.com/heavy-resume/heavy-stack and see if it fixes some line number issues I was seeing updated the nested async functions called by reactpy

edit: I'll wait for @breuleux 's fix to decorators to see if they fix all the line number issues I encountered.

Comment on lines +863 to +868
def apply_assertion_rewrite(self, ast_func, glb):
from _pytest.assertion.rewrite import AssertionRewriter

# We only want to apply assertion rewrites if we're running in a pytest context
if not CONFIG.rewrite_asserts_to_pytest_asserts:
return ast_func

Choose a reason for hiding this comment

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

I think this will work as long as its off by default. One consideration is that a more robust solution is the ability to register hooks for reevaluate, then you would loop through those. I would for sure have that in a separate PR.

Copy link

Choose a reason for hiding this comment

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

The more I think about it, the more I think jurigged shouldn't own this, but rather just provide capabilities for other libraries to hook into it, which would be the hooks approach in this case. The pytest hot reloader library should own fixing pytest (which it already does), but it also should be able to do so without having to resort to monkey patching jurigged.

# seem to have a way to add rewrite hooks
# TODO: We should fix this in jurigged itself, but porting over from here for now:
# https://github.com/breuleux/jurigged/issues/29
new_node = self.apply_assertion_rewrite(new_node, glb)

Choose a reason for hiding this comment

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

consider moving the CONFIG check here so that the apply_assertion_rewrite logic is simpler. You wouldn't expect a function with that name to not do what it says.

Copy link

Choose a reason for hiding this comment

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

This comment is obsolete; after having time to stew on this, I don't think jurigged should own fixing pytest, it just needs to provide a means to do so without monkey patching the library.

Comment on lines +699 to +703
# patch: There's an off-by-one bug coming from somewhere in jurigged.
# This affects replaced functions. When line numbers are wrong
# the debugger and inspection logic doesn't work as expected.
# TODO: We should fix this in jurigged itself, but porting over from here for now:
# https://github.com/breuleux/jurigged/issues/29

Choose a reason for hiding this comment

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

IIRC there's some places where the line number is tweaked. It would make sense to fix it there, but I would want @breuleux to comment

Choose a reason for hiding this comment

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

As per #29 (comment) this was likely compensating for an issue with decorators and we can remove this since it wasn't a proper fix anyways

Comment on lines +50 to +68
def _get_future_compiler_flags(glb) -> int:
"""use future feature names to get any that are set in the global namespace

`or` these together to get the flags for the compile function
"""
flags = 0
for key in glb.keys() & _future_feature_names:
with suppress(Exception):
flags |= glb[key].compiler_flag
return flags


def _compile_with_flags(node, mode, filename, glb):
"""compile the node with the future flags set in the global namespace

basically, respect `from __future__ import annotations`
"""
flags = _get_future_compiler_flags(glb)
return compile(node, mode=mode, filename=filename, flags=flags)

Choose a reason for hiding this comment

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

I would suggest removing this from this PR since its already in a different one.

@breuleux
Copy link
Owner

Future annotations have been added from the other PR and I added the support for watch args in a different commit, done differently (e.g. -w x -w y instead of -w x y). For what remains, I'm not sure -- I'd rather not special case pytest rewrites and I haven't found a reliable fix for line numbers.

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.

None yet

3 participants