Skip to content

Treat type aliases as typing-only expressions#7968

Merged
charliermarsh merged 1 commit intomainfrom
charlie/tch-pep
Oct 16, 2023
Merged

Treat type aliases as typing-only expressions#7968
charliermarsh merged 1 commit intomainfrom
charlie/tch-pep

Conversation

@charliermarsh
Copy link
Member

Summary

Given type RecordOrThings = Record | int | str, the right-hand side won't be evaluated at runtime. Same goes for Record in type RecordCallback[R: Record] = Callable[[R], None]. This PR modifies the visitation logic to treat them as typing-only.

Closes #7966.

@charliermarsh charliermarsh added the bug Something isn't working label Oct 16, 2023
@charliermarsh charliermarsh enabled auto-merge (squash) October 16, 2023 00:06
@charliermarsh charliermarsh merged commit b6e75e5 into main Oct 16, 2023
@charliermarsh charliermarsh deleted the charlie/tch-pep branch October 16, 2023 00:09
@github-actions
Copy link
Contributor

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

charliermarsh added a commit that referenced this pull request Oct 18, 2023
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

In #7968, I introduced a
regression whereby we started to treat imports used _only_ in type
annotation bounds (with `__future__` annotations) as unused.

The root of the issue is that I started using `visit_annotation` for
these bounds. So we'd queue up the bound in the list of deferred type
parameters, then when visiting, we'd further queue it up in the list of
deferred type annotations... Which we'd then never visit, since deferred
type annotations are visited _before_ deferred type parameters.

Anyway, the better solution here is to use a dedicated flag for these,
since they have slightly different behavior than type annotations.

I've also fixed what I _think_ is a bug whereby we previously failed to
resolve `Callable` in:

```python
type RecordCallback[R: Record] = Callable[[R], None]

from collections.abc import Callable
```

IIUC, the values in type aliases should be evaluated lazily, like type
parameters.

Closes #8017.

## Test Plan

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TCH false positive with PEP-695 upper bound specification

1 participant