Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Apr 22, 2025

Summary

This is a first step toward global support in red-knot (#15385). I went through all the matches for global in the mypy/test-data directory, but I didn't find anything too interesting that wasn't already covered by @carljm's suggestions on Discord. I still pulled in a couple of cases for a little extra variety. I also included a section from the PLE0118 tests in ruff that will become syntax errors once #17463 is merged and we handle global statements.

I don't think I figured out how to use @Todo properly, so please let me know if I need to fix that. I hope this is a good start to the test suite otherwise.

@ntBre ntBre added the ty Multi-file analysis & type inference label Apr 22, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Apr 22, 2025

mypy_primer results

No ecosystem changes detected ✅

@ntBre ntBre force-pushed the brent/global-test-suite branch from 61f4ccc to 88af630 Compare April 22, 2025 19:57
@ntBre ntBre marked this pull request as ready for review April 22, 2025 20:01
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Awesome, thank you! A few comments, mostly just about adapting to our "house style" for TODO comments in mdtests.

@ntBre
Copy link
Contributor Author

ntBre commented Apr 23, 2025

Thanks for the review, it was very helpful! I think this should be in a much better state now.

@AlexWaygood AlexWaygood removed their request for review April 23, 2025 15:02
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@ntBre ntBre merged commit bfc1650 into main Apr 23, 2025
33 checks passed
@ntBre ntBre deleted the brent/global-test-suite branch April 23, 2025 21:18
dcreager added a commit that referenced this pull request Apr 24, 2025
* main:
  [red-knot] fix collapsing literal and its negation to object (#17605)
  [red-knot] Add more tests for protocols (#17603)
  [red-knot] Ban direct instantiations of `Protocol` classes (#17597)
  [`pyupgrade`] Preserve parenthesis when fixing native literals containing newlines (`UP018`) (#17220)
  [`airflow`] fix typos (`AIR302`, `AIR312`) (#17574)
  [red-knot] Special case `@abstractmethod` for function type (#17591)
  [red-knot] Emit diagnostics for isinstance() and issubclass() calls where a non-runtime-checkable protocol is the second argument (#17561)
  [red-knot] Infer the members of a protocol class (#17556)
  [red-knot] Add `FunctionType::to_overloaded` (#17585)
  [red-knot] Add mdtests for `global` statement (#17563)
  [syntax-errors] Make duplicate parameter names a semantic error (#17131)
ntBre added a commit that referenced this pull request May 8, 2025
… syntax error (#17637)

Summary
--

This PR resolves both the typing-related and syntax error TODOs added in
#17563 by tracking a set of `global` bindings for each scope. As
discussed below, we avoid the additional AST traversal from ruff by
collecting `Name`s from `global` statements while building the semantic
index and emit a syntax error if the `Name` is already bound in the
current scope at the point of the `global` statement. This has the
downside of separating the error from the `SemanticSyntaxChecker`, but I
plan to explore using this approach in the `SemanticSyntaxChecker`
itself as a follow-up. It seems like this may be a better approach for
ruff as well.

Test Plan
--

Updated all of the related mdtests to remove the TODOs (and add quotes I
forgot on the messages).

There is one remaining TODO, but it requires `nonlocal` support, which
isn't even incorporated into the `SemanticSyntaxChecker` yet.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Carl Meyer <carl@astral.sh>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants