Commit Graph

13636 Commits

Author SHA1 Message Date
Leandro Braga
20d80edfc2 [flake8-bugbear] Mark fix unsafe when it would remove comments (B033) (#22632)
The B033 erroneously removes comments in safe fix.

By running
```console
$ cargo run -p ruff  -- check --select B033 - << 'EOF'
{
  1,
  # comment
  1
}
EOF
```

We get:

# Before

```console
B033 [*] Sets should not contain duplicate item `1`
 --> -:4:3
  |
2 |   1,
3 |   # comment
4 |   1
  |   ^
5 | }
  |
help: Remove duplicate item

Found 1 error.
[*] 1 fixable with the `--fix` option.
```

# After
```console
B033 Sets should not contain duplicate item `1`
 --> -:4:3
  |
2 |   1,
3 |   # comment
4 |   1
  |   ^
5 | }
  |
help: Remove duplicate item

Found 1 error.
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).
```

Closes #22629
2026-01-16 19:07:19 -05:00
Alex Waygood
717d024ea9 [ty] Generalize union-type subtyping fast path (#22495) 2026-01-16 22:09:06 +00:00
Leandro Braga
b80d8ff6ff [ruff] Detect duplicate entries in __all__ (RUF068) (#22114)
Hello,

This MR adds a new rule and its fix, `RUF069`,
`DuplicateEntryInDunderAll`. I'm using `RUF069` because we already have
[RUF068](https://github.com/astral-sh/ruff/pull/20585) and
[RUF069](https://github.com/astral-sh/ruff/pull/21079#issuecomment-3493839453)
in the works.

The rule job is to prevent users from accidentally adding duplicate
entries to `__all__`, which, for example, can result from copy-paste
mistakes.

It deals with the following syntaxes:

```python
__all__: list[str] = ["a", "a"]
__all__: typing.Any = ("a", "a")
__all__.extend(["a", "a"])
__all__ += ["a", "a"]
```

But it does not keep track of `__all__` contents, meaning the following
code snippet is a false negative:
```python
class A: ...

__all__ = ["A"]
__all__.extend(["A"])
```

## Violation Example

```console
RUF069 `__all__` contains duplicate entries
 --> RUF069.py:2:17
  |
1 | __all__ = ["A", "A", "B"]
  |                 ^^^
help: Remove duplicate entries from `__all__`
1 | __all__ = ["A", "B"]
  - __all__ = ["A", "A", "B"]
```

## Ecosystem Report

The `ruff-ecosystem` results contain seven violations in four projects,
all of them seem like true positives, with one instance appearing to be
an actual bug.

This [code
snippet](90d855985b/stubs/reportlab/reportlab/lib/rltempfile.pyi (L4))
from `reportlab` contains the same entry twice instead of exporting both
functions.

```python
def get_rl_tempdir(*subdirs: str) -> str: ...
def get_rl_tempfile(fn: str | None = None) -> str: ...

__all__ = ("get_rl_tempdir", "get_rl_tempdir")
```

Closes [#21945](https://github.com/astral-sh/ruff/issues/21945)

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-01-16 14:58:06 -05:00
Amethyst Reese
337e3ebd27 Combine suppression code diagnostics (#22613)
# Summary

- Report a single combined UnusedNOQA diagnostic when any range
suppression
  has one or more unused, disabled, or duplicate lint codes.
- Report a single combined InvalidRuleCode diagnostic for any range
suppression
  with one or more invalid lint codes.

# Test Plan

Updated fixtures and snapshots

Fixes #22429, #21873
2026-01-16 11:08:48 -08:00
Dex Devlon
5c97b6ef40 [ty] Correct return type for synthesized NamedTuple.__new__ methods (#22625) 2026-01-16 18:56:56 +00:00
Alex Waygood
ed355b6173 [ty] Rename some narrowing-related machinery (#22618) 2026-01-16 17:10:33 +00:00
Alex Waygood
a2b383842a [ty] Allow ... as a default value for any parameter if the function is in an if TYPE_CHECKING block (#22624) 2026-01-16 16:32:38 +00:00
Micha Reiser
a636fbe91c Remove ty_python_semantic dependency from ruff_graph (#22623)
Co-authored-by: Claude <noreply@anthropic.com>
2026-01-16 16:17:05 +01:00
konsti
10fd3b2415 Use explicit manylinux/musllinux targets and better pre-upload checks (#22477)
This ensures that changes to the targets are intentional and explicit.

See also https://github.com/astral-sh/ty/pull/2393 and
https://github.com/astral-sh/uv/pull/17358
2026-01-16 14:26:52 +00:00
Micha Reiser
eb99f80a02 [ty] Extract ty_site_packages crate (#22622) 2026-01-16 15:02:47 +01:00
Alex Waygood
3c323559ed More fixes to scripts/conformance.py (#22616) 2026-01-16 12:47:39 +00:00
Alex Waygood
0cc3a775cd Tell Claude to prefer using #[expect()] over #[allow()] where possible (#22617) 2026-01-16 12:36:32 +00:00
Alex Waygood
28bfcf82b8 [ty] Make ModuleType and object attributes available on namespace packages (#22606)
## Summary

Currently we don't think that namespace packages (e.g. `google` after
you've pip-installed `google-cloud-ndb`) have attributes such as
`__file__`, `__name__`, etc. This PR fixes that.

## Test Plan

Mdtests and snapshots.
2026-01-16 08:42:58 +00:00
Micha Reiser
4adbf7798d [ty] Fix --force-exclude when excluding entire directories (#22595) 2026-01-16 09:23:26 +01:00
Caíque Porfirio
0ce5ce4de1 [ruff] Add exception for ctypes.Structure._fields_ (RUF012) (#22559)
Closes #22166 
## Summary
Adds an exception for `ctypes.Structure._fields_` to the rule RUF012 as
it has it's own way of enforcing immutability:

> The fields class variable can only be set once. Later assignments will
raise an
[AttributeError](https://docs.python.org/3/library/exceptions.html#AttributeError).

---------

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-01-15 20:49:59 +00:00
Ibraheem Ahmed
78d1343583 [ty] Infer implicit type of cls in __new__ methods (#22584)
## Summary

Resolves https://github.com/astral-sh/ty/issues/2489.
2026-01-15 15:42:16 -05:00
Amethyst Reese
c696ef4025 Skip walking all tokens when loading range suppressions (#22446)
- Adds `Tokens::split_at()` to get tokens before/after an offset.
- Updates `Suppressions::load_from_tokens` to take an `Indexer` and use
comment ranges to minimize the need for walking tokens looking for
indent/dedent.

Adapted from
https://github.com/astral-sh/ruff/pull/21441#pullrequestreview-3503773083

Fixes #22087
2026-01-15 12:35:24 -08:00
Charlie Marsh
b4b8299d6c [ty] Make NamedTuple(...) and namedtuple(...) calls stricter (#22601)
## Summary

Closes https://github.com/astral-sh/ty/issues/2513.
0.14.13
2026-01-15 18:24:25 +00:00
Brent Westbrook
fd9f87d02d Bump 0.14.13 (#22604) 2026-01-15 12:53:20 -05:00
Andrew Gallant
048f02fb6b [ty] Remove redundant re-exports that share the same top-most module
The implementation here is (to me) surprisingly complicated. The main
complications are:

1. Trying to limit the redundant detection to as few of the symbols
we extract as possible. In particular, while I haven't done benchmarking
on this, I perceive the redundancy detection to be somewhat expensive
and auto-import can return lots of symbols. So we're careful to only do
this extra checking on (typically) small groups of symbols that could
possibly be merged.

2. Even by restricting our work, this merging process could still be
called quite a bit. (Thousands of times in my "standard data scienc-y
test environment.") So I went out of my way to amortize allocs.

3. Re-exports can form a chain and we want to find all of them.

4. We (probably) don't want to remove redundant re-exports unless
they share the same top-level module. Otherwise, e.g., a library
that re-exports another library's symbols could have all of its
re-exports dropped.

5. We want to only keep the top-most re-exports, and there may be
multiple such re-exports. So we keep all of them.

6. We can't assume anything about the relationship of re-exports
and the original definition. A re-export could be deeper in the
module hierarchy than the original definition or above it.
2026-01-15 12:31:07 -05:00
Andrew Gallant
a72af10cf8 [ty] Add ModuleName::top
The invariants established by the constructors for `ModuleName`
guarantee that this is always available. It's useful for
determining the "top level" module for where a symbol lives.
2026-01-15 12:31:07 -05:00
Andrew Gallant
2f34836318 [ty] Add test capturing redundant re-exports from pandas
This roughly mimics how pandas defines and re-exports
`read_csv` at the top-level module.
2026-01-15 12:31:07 -05:00
Andrew Gallant
db9eee7b06 [ty] Attach origin module on to re-exported symbols
This information should let us filter out (or rather, merge)
re-exported symbols across a package hierarchy for the purposes
of auto-completions.
2026-01-15 12:31:07 -05:00
Andrew Gallant
2cbd68ab70 [ty] Move fully qualified name into auto-import implementation
Previously, we were constructing this at a higher level layer. But this
commit pushes it down a layer of abstraction. This shouldn't result in
constructing the fully qualified name any more frequently than we
previously did. Namely, we're still only doing it for symbols that match
the caller provided search query.

The motivation here is that we want to do some de-duplication based on
module name, and having the fully qualified name of a symbol seems like
the most straight-forward way to go about this. (We'll need one more
piece of information that we add in a subsequent commit.)
2026-01-15 12:31:07 -05:00
Brent Westbrook
2a0946760f Add trailing slashes to npm publish paths (#22603)
Summary
--

The WASM builds in the 0.14.12 release were failing with an error about
trying
to publish over an existing version 5.8.1 ([example]). This seems to be
because
it's trying to publish a package named `pkg` instead of the `pkg`
directory.

Micha found that a trailing slash causes `npm` to interpret the path
correctly.

Test Plan
--

I think we can test this with a dry run of the release workflow,
otherwise
another release attempt for 0.14.12

[example]:
https://github.com/astral-sh/ruff/actions/runs/21037103249/job/60492735499#step:5:281
2026-01-15 12:18:22 -05:00
Rob Hand
eca58ca1d3 [ty] Override __file__ to str when applicable on imported modules (#22333) 2026-01-15 17:08:50 +00:00
Alex Waygood
aa9f1b27fc Further improvements to typing conformance script (#22596) 2026-01-15 15:49:02 +00:00
Brent Westbrook
f9dd973b13 Bump 0.14.12 (#22602) 2026-01-15 10:41:03 -05:00
Bhuminjay Soni
b2d57ddaa5 [ty]: consolidate type[] types in a union when displaying them in diagnostics (#22592) 2026-01-15 15:20:58 +00:00
Charlie Marsh
de954b6fa4 [ty] Support starred unpacking in class bases (#22591)
## Summary

Closes https://github.com/astral-sh/ty/issues/2492.
2026-01-15 10:17:11 -05:00
Charlie Marsh
fd7cc1f9c9 [ty] Validate field names for typing.NamedTuple(...) (#22599)
## Summary

Closes https://github.com/astral-sh/ty/issues/2511.
2026-01-15 09:15:45 -05:00
RasmusNygren
2a29ce3e41 [ty] Better class def completions (#22571)
## Summary

Prefer completions of type `ClassLiteral` inside class-def context.

Fixes https://github.com/astral-sh/ty/issues/1776

## Test Plan
New completion-eval test that has incorrect ranking on main but correct
after this patch.
2026-01-15 08:31:04 -05:00
Will Duke
9c67b2acd9 [ty] Try fixing conformance workflow (#22593)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-01-15 12:38:13 +00:00
github-actions[bot]
1f9b44f7ce [ty] Sync vendored typeshed stubs (#22590)
Co-authored-by: typeshedbot <>
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2026-01-15 12:37:30 +00:00
Jason K Hall
056d935fcd wasm: Require explicit logging initialization (#22587)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-01-15 08:44:54 +00:00
Micha Reiser
c3ba638ef5 [ty] Better handling for neutral conformence test changes (#22585) 2026-01-15 08:53:11 +01:00
Carl Meyer
87110c2892 [ty] Fix PEP 695 type aliases not expanding in overload resolution (#22589)
Fixes https://github.com/astral-sh/ty/issues/2488

When a type alias is defined using PEP 695's `type` statement syntax
(e.g., `type Array = Eager | Lazy`), overload resolution was failing
because the type alias was not being expanded into its underlying union
type.

This fix updates both `expand_type` and `is_expandable_type` in
`arguments.rs` to handle `Type::TypeAlias` by recursively checking and
expanding the alias's value type.
2026-01-15 06:25:08 +00:00
Alex Waygood
5a18e93d65 [ty] Make special cases for subscript inference exhaustive (#22035)
## Summary

Fixes https://github.com/astral-sh/ty/issues/2015. We weren't recursing
into the value of a type alias when we should have been.

There are situations where we should also be recursing into the
bounds/constraints of a typevar. I initially tried to do that as well in
this PR, but that seems... trickier. For now I'm cutting scope; this PR
does, however, add several failing tests for those cases.

## Test Plan

added mdtests
2026-01-15 00:18:54 +00:00
Илья Любавский
5d99ef9d95 [flake8-blind-except] Allow more logging methods (BLE001) (#22057)
## Summary
Fix issue #21889 by checking that the logging method is one of the
``debug``, ``info``, ``warning``, ``error``, ``exception``,
``critical``, ``log`` methods that support ``exc_info`` passing. Also
fixed the behavior in which ``exc_info`` was considered passed only when
it was equal to the literal ``True``, now the ``Truthiness`` of the
expression is checked (we will leave additional checks to type checkers)

## Test Plan
Additional snapshot tests have been added for all logging functions, as
well as tests in which an exception object is passed as ``exc_info``

---------

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-01-14 21:18:13 +00:00
liam
1bd5ff77d0 [ruff] Respect lint.pydocstyle.property-decorators in RUF066 (#22515)
Resolves #22216.

This diff makes `RUF066` (`property-without-return`) respect the
`lint.pydocstyle.property-decorators` setting. `RUF066` is now
consistent with other rules that check for property methods like
`PLR0206`, `D401`, `PLR6301`, and `DOC201`
2026-01-14 16:02:24 -05:00
Alex Waygood
b79e9bac14 [ty] Use let-chains more (#22580)
## Summary

just a little refactor.

Edit: okay, I removed a period at the end of a diagnostic message, which
I guess changes a _lot_ of diagnostic messages.
2026-01-14 19:56:07 +00:00
Will Duke
7e8eba2572 [ty] Update conformance workflow to use comparison script (#22504)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-01-14 19:06:20 +00:00
Charlie Marsh
b07a53ab68 [ty] Emit diagnostics for invalid dynamic namedtuple fields (#22575)
## Summary

Removes some TODOs from the dynamic namedtuple implementation.
2026-01-14 18:42:35 +00:00
Charlie Marsh
87eec9bb51 [ty] Show dynamic NamedTuple defaults in signature (#22574)
## Summary

Follow-up from https://github.com/astral-sh/ruff/pull/22327.
2026-01-14 18:28:23 +00:00
Micha Reiser
eaed0d9b5c [ty] Fix flaky completions (#22576) 2026-01-14 19:23:38 +01:00
Charlie Marsh
eb96456e1e [ty] Synthesize an empty __slots__ for named tuples (#22573)
## Summary

Closes https://github.com/astral-sh/ty/issues/2490.
2026-01-14 18:22:27 +00:00
Charlie Marsh
3e0299488e [ty] Add support for functional namedtuple creation (#22327)
## Summary

This PR is intended to demonstrate how the pattern established in
https://github.com/astral-sh/ruff/pull/22291 generalizes to other class
"kinds".

Closes https://github.com/astral-sh/ty/issues/1049.

---------

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2026-01-14 17:41:04 +00:00
Alex Waygood
7f0ce3e88d [ty] Infer type[Unknown] for calls to type() when overload evaluation is ambiguous (#22569)
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
2026-01-14 14:23:07 +00:00
Charlie Marsh
ba0736385d [ty] Add a diagnostic for non-decorator uses of final (#22555)
## Summary

See:
https://github.com/astral-sh/ruff/pull/22499#discussion_r2687263390.
2026-01-14 09:14:59 -05:00
Charlie Marsh
e41f045ec5 [ty] Emit diagnostics for invalid base classes in type(...) (#22499)
## Summary

Tackles a few TODOs from https://github.com/astral-sh/ruff/pull/22291.
2026-01-14 08:56:04 -05:00