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

Fix imports in a module containing __future__ #387

Closed

Conversation

vthemelis
Copy link
Contributor

Fixes: #385

@coveralls
Copy link

coveralls commented Aug 26, 2023

Coverage Status

coverage: 97.811% (-0.008%) from 97.819% when pulling da99fe3 on vthemelis:injecting-code-before-future into 068edfd on agronholm:master.

@vthemelis vthemelis force-pushed the injecting-code-before-future branch 4 times, most recently from 3a3de78 to 27275eb Compare August 26, 2023 20:53
@agronholm
Copy link
Owner

Are the test suites passing for you locally?

@vthemelis vthemelis force-pushed the injecting-code-before-future branch 4 times, most recently from cb25bd5 to 7dfb1ef Compare August 26, 2023 21:04
@vthemelis
Copy link
Contributor Author

Are the test suites passing for you locally?

Yes, I can get a clean run with tox. I'm just having trouble with linters being applied to places where they shouldn't by pre-commit.ci though which leads to redness on the PR.

@vthemelis
Copy link
Contributor Author

@agronholm All 💚 now!

@agronholm
Copy link
Owner

Yes, I can get a clean run with tox. I'm just having trouble with linters being applied to places where they shouldn't by pre-commit.ci though which leads to redness on the PR.

Installing pre-commit locally would have made that easier..

@agronholm
Copy link
Owner

The test for this really belongs in test_transformer.py, not in dummymodule.py.

@vthemelis
Copy link
Contributor Author

Installing pre-commit locally would have made that easier..

How do you run it locally? I tried pre-commit run but it was no-op for me.

@vthemelis
Copy link
Contributor Author

The test for this really belongs in test_transformer.py, not in dummymodule.py.

It seemed like dummymodule.py gave me pretty good integration coverage already so I went this way. Do you want me to add unit tests explicitly to test_transformer.py too?

@agronholm
Copy link
Owner

First of all, you should do pre-commit install which ensures that the checks are run when you do git commit. If you want to run the checks without the git hook, do pre-commit run -a (-a meaning "run for all files").

@agronholm
Copy link
Owner

The problem seems to stem from __post_init__() only setting the right injection index if self.node is set, which it isn't when a module is being transformed by the import hook.

@agronholm
Copy link
Owner

I'm likely to reject this PR in favor of an alternate approach and an alternate test.

@agronholm
Copy link
Owner

Here's a test that fails without any patch:

def test_respect_future_import() -> None:
    # Regression test for #385
    node = parse(
        dedent(
            '''
            from __future__ import annotations
            
            def foo() -> int:
                return 1
            '''
        )
    )
    TypeguardTransformer().visit(node)
    assert (
        unparse(node)
        == dedent(
            '''
            from __future__ import annotations
            from typeguard import TypeCheckMemo
            from typeguard._functions import check_return_type

            def foo() -> int:
                memo = TypeCheckMemo(globals(), locals())
                return check_return_type('foo', 1, int, memo)
            '''
        ).strip()
    )

@vthemelis
Copy link
Contributor Author

I'm likely to reject this PR in favor of an alternate approach and an alternate test.

Fair enough! Let me know if you want me to help.

@vthemelis
Copy link
Contributor Author

Here's a test that fails without any patch:

Thanks for this! Seems to pass with my changes. Shall I commit it?

@agronholm
Copy link
Owner

I'm still considering my options. If I figure out a better way, I'll just push that and close this PR. Either way I'll let you know.

@vthemelis
Copy link
Contributor Author

I'm still considering my options. If I figure out a better way, I'll just push that and close this PR. Either way I'll let you know.

Sounds good. I added the test in case we merge this PR.

@agronholm
Copy link
Owner

Alright, I resolved this via c11057a. Thanks for your initiative on the matter.

@agronholm agronholm closed this Aug 26, 2023
@vthemelis
Copy link
Contributor Author

Thanks! Would it be possible to release the latest changes?

@agronholm
Copy link
Owner

Thanks! Would it be possible to release the latest changes?

Done.

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.

"SyntaxError: from __future__ imports must occur at the beginning of the file" after comments
3 participants