mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 03:48:29 +00:00
[ty] Use "cannot" consistently over "can not" (#21255)
This commit is contained in:
parent
e8c35b9704
commit
39f105bc4a
24 changed files with 39 additions and 39 deletions
|
|
@ -118,7 +118,7 @@ def takes_other_protocol(f: OtherProtocol): ...
|
|||
takes_other_protocol(SubclassOfAny())
|
||||
```
|
||||
|
||||
A subclass of `Any` cannot be assigned to literal types, since those can not be subclassed:
|
||||
A subclass of `Any` cannot be assigned to literal types, since those cannot be subclassed:
|
||||
|
||||
```py
|
||||
from typing import Any, Literal
|
||||
|
|
|
|||
|
|
@ -1878,7 +1878,7 @@ date.day = 8
|
|||
date.month = 4
|
||||
date.year = 2025
|
||||
|
||||
# error: [unresolved-attribute] "Can not assign object of type `Literal["UTC"]` to attribute `tz` on type `Date` with custom `__setattr__` method."
|
||||
# error: [unresolved-attribute] "Cannot assign object of type `Literal["UTC"]` to attribute `tz` on type `Date` with custom `__setattr__` method."
|
||||
date.tz = "UTC"
|
||||
```
|
||||
|
||||
|
|
@ -1894,10 +1894,10 @@ class Frozen:
|
|||
existing: int = 1
|
||||
|
||||
def __setattr__(self, name, value) -> Never:
|
||||
raise AttributeError("Attributes can not be modified")
|
||||
raise AttributeError("Attributes cannot be modified")
|
||||
|
||||
instance = Frozen()
|
||||
instance.non_existing = 2 # error: [invalid-assignment] "Can not assign to unresolved attribute `non_existing` on type `Frozen`"
|
||||
instance.non_existing = 2 # error: [invalid-assignment] "Cannot assign to unresolved attribute `non_existing` on type `Frozen`"
|
||||
instance.existing = 2 # error: [invalid-assignment] "Cannot assign to attribute `existing` on type `Frozen` whose `__setattr__` method returns `Never`/`NoReturn`"
|
||||
```
|
||||
|
||||
|
|
@ -1949,7 +1949,7 @@ def flag() -> bool:
|
|||
class Frozen:
|
||||
if flag():
|
||||
def __setattr__(self, name, value) -> Never:
|
||||
raise AttributeError("Attributes can not be modified")
|
||||
raise AttributeError("Attributes cannot be modified")
|
||||
|
||||
instance = Frozen()
|
||||
instance.non_existing = 2 # error: [invalid-assignment]
|
||||
|
|
|
|||
|
|
@ -194,7 +194,7 @@ class_with_descriptor_dunder = ClassWithDescriptorDunder()
|
|||
reveal_type(class_with_descriptor_dunder[0]) # revealed: str
|
||||
```
|
||||
|
||||
## Dunders can not be overwritten on instances
|
||||
## Dunders cannot be overwritten on instances
|
||||
|
||||
If we attempt to overwrite a dunder method on an instance, it does not affect the behavior of
|
||||
implicit dunder calls:
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ class E(metaclass=Meta): ...
|
|||
reveal_type(inspect.getattr_static(E, "attr")) # revealed: int
|
||||
```
|
||||
|
||||
Metaclass attributes can not be added when probing an instance of the class:
|
||||
Metaclass attributes cannot be added when probing an instance of the class:
|
||||
|
||||
```py
|
||||
reveal_type(inspect.getattr_static(E(), "attr", "non_existent")) # revealed: Literal["non_existent"]
|
||||
|
|
|
|||
|
|
@ -308,7 +308,7 @@ reveal_type(C.f) # revealed: bound method <class 'C'>.f(arg: int) -> str
|
|||
reveal_type(C.f(1)) # revealed: str
|
||||
```
|
||||
|
||||
The method `f` can not be accessed from an instance of the class:
|
||||
The method `f` cannot be accessed from an instance of the class:
|
||||
|
||||
```py
|
||||
# error: [unresolved-attribute] "Object of type `C` has no attribute `f`"
|
||||
|
|
|
|||
|
|
@ -424,7 +424,7 @@ from dataclasses import dataclass
|
|||
class MyFrozenClass: ...
|
||||
|
||||
frozen = MyFrozenClass()
|
||||
frozen.x = 2 # error: [invalid-assignment] "Can not assign to unresolved attribute `x` on type `MyFrozenClass`"
|
||||
frozen.x = 2 # error: [invalid-assignment] "Cannot assign to unresolved attribute `x` on type `MyFrozenClass`"
|
||||
```
|
||||
|
||||
A diagnostic is also emitted if a frozen dataclass is inherited, and an attempt is made to mutate an
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ def outer_generator():
|
|||
## `yield from` with a custom iterable
|
||||
|
||||
`yield from` can also be used with custom iterable types. In that case, the type of the `yield from`
|
||||
expression can not be determined
|
||||
expression cannot be determined
|
||||
|
||||
```py
|
||||
from typing import Generator, TypeVar, Generic
|
||||
|
|
|
|||
|
|
@ -130,7 +130,7 @@ static_assert(has_member(C, "base_attr"))
|
|||
static_assert(not has_member(C, "non_existent"))
|
||||
```
|
||||
|
||||
But instance attributes can not be accessed this way:
|
||||
But instance attributes cannot be accessed this way:
|
||||
|
||||
```py
|
||||
static_assert(not has_member(C, "instance_attr"))
|
||||
|
|
|
|||
|
|
@ -444,7 +444,7 @@ def _(
|
|||
reveal_type(i07) # revealed: Never
|
||||
reveal_type(i08) # revealed: Never
|
||||
|
||||
# `bool` is final and can not be subclassed, so `type[bool]` is equivalent to `Literal[bool]`, which
|
||||
# `bool` is final and cannot be subclassed, so `type[bool]` is equivalent to `Literal[bool]`, which
|
||||
# is disjoint from `type[str]`:
|
||||
def example_type_bool_type_str(
|
||||
i: Intersection[type[bool], type[str]],
|
||||
|
|
|
|||
|
|
@ -390,7 +390,7 @@ static_assert(not is_single_valued(Literal["a"] | Literal["b"]))
|
|||
|
||||
We use `TypeOf` to get the inferred type of an expression. This is useful when we want to refer to
|
||||
it in a type expression. For example, if we want to make sure that the class literal type `str` is a
|
||||
subtype of `type[str]`, we can not use `is_subtype_of(str, type[str])`, as that would test if the
|
||||
subtype of `type[str]`, we cannot use `is_subtype_of(str, type[str])`, as that would test if the
|
||||
type `str` itself is a subtype of `type[str]`. Instead, we can use `TypeOf[str]` to get the type of
|
||||
the expression `str`:
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class Small(Medium): ...
|
|||
static_assert(is_assignable_to(Any | Medium, Big))
|
||||
static_assert(is_assignable_to(Any | Medium, Medium))
|
||||
|
||||
# `Any | Medium` is at least as large as `Medium`, so we can not assign it to `Small`:
|
||||
# `Any | Medium` is at least as large as `Medium`, so we cannot assign it to `Small`:
|
||||
static_assert(not is_assignable_to(Any | Medium, Small))
|
||||
```
|
||||
|
||||
|
|
@ -84,7 +84,7 @@ static_assert(is_assignable_to(Small, Intersection[Any, Medium]))
|
|||
static_assert(is_assignable_to(Medium, Intersection[Any, Medium]))
|
||||
```
|
||||
|
||||
`Any & Medium` is no larger than `Medium`, so we can not assign `Big` to it. There is no possible
|
||||
`Any & Medium` is no larger than `Medium`, so we cannot assign `Big` to it. There is no possible
|
||||
materialization of `Any & Medium` that would make it as big as `Big`:
|
||||
|
||||
```py
|
||||
|
|
|
|||
|
|
@ -32,8 +32,8 @@ static_assert(not is_singleton(Literal[1]))
|
|||
static_assert(not is_singleton(Literal[54165]))
|
||||
```
|
||||
|
||||
This has implications for type-narrowing. For example, you can not use the `is not` operator to
|
||||
check whether a variable has a specific integer literal type, but this is not a recommended practice
|
||||
This has implications for type-narrowing. For example, you cannot use the `is not` operator to check
|
||||
whether a variable has a specific integer literal type, but this is not a recommended practice
|
||||
anyway.
|
||||
|
||||
```py
|
||||
|
|
@ -44,7 +44,7 @@ def f(x: int):
|
|||
reveal_type(x) # revealed: Literal[54165]
|
||||
|
||||
if x is not 54165:
|
||||
# But here, we can not narrow the type (to `int & ~Literal[54165]`), because `x` might also
|
||||
# But here, we cannot narrow the type (to `int & ~Literal[54165]`), because `x` might also
|
||||
# have the value `54165`, but a different object identity.
|
||||
reveal_type(x) # revealed: int
|
||||
```
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class C(B1, B2): ...
|
|||
# ... which lies in their intersection:
|
||||
static_assert(is_subtype_of(C, Intersection[B1, B2]))
|
||||
|
||||
# However, if a class is marked final, it can not be subclassed ...
|
||||
# However, if a class is marked final, it cannot be subclassed ...
|
||||
@final
|
||||
class FinalSubclass(A): ...
|
||||
|
||||
|
|
|
|||
|
|
@ -680,7 +680,7 @@ def _(p: Person) -> None:
|
|||
reveal_type(p.__class__) # revealed: <class 'dict[str, object]'>
|
||||
```
|
||||
|
||||
Also, the "attributes" on the class definition can not be accessed. Neither on the class itself, nor
|
||||
Also, the "attributes" on the class definition cannot be accessed. Neither on the class itself, nor
|
||||
on inhabitants of the type defined by the class:
|
||||
|
||||
```py
|
||||
|
|
@ -714,7 +714,7 @@ reveal_type(Person.__required_keys__) # revealed: frozenset[str]
|
|||
reveal_type(Person.__optional_keys__) # revealed: frozenset[str]
|
||||
```
|
||||
|
||||
These attributes can not be accessed on inhabitants:
|
||||
These attributes cannot be accessed on inhabitants:
|
||||
|
||||
```py
|
||||
def _(person: Person) -> None:
|
||||
|
|
@ -723,7 +723,7 @@ def _(person: Person) -> None:
|
|||
person.__optional_keys__ # error: [unresolved-attribute]
|
||||
```
|
||||
|
||||
Also, they can not be accessed on `type(person)`, as that would be `dict` at runtime:
|
||||
Also, they cannot be accessed on `type(person)`, as that would be `dict` at runtime:
|
||||
|
||||
```py
|
||||
def _(person: Person) -> None:
|
||||
|
|
|
|||
|
|
@ -187,8 +187,8 @@ python-platform = "all"
|
|||
|
||||
If `python-platform` is set to `all`, we treat the platform as unspecified. This means that we do
|
||||
not infer a literal type like `Literal["win32"]` for `sys.platform`, but instead fall back to
|
||||
`LiteralString` (the `typeshed` annotation for `sys.platform`). This means that we can not
|
||||
statically determine the truthiness of a branch like `sys.platform == "win32"`.
|
||||
`LiteralString` (the `typeshed` annotation for `sys.platform`). This means that we cannot statically
|
||||
determine the truthiness of a branch like `sys.platform == "win32"`.
|
||||
|
||||
See <https://github.com/astral-sh/ruff/issues/16983#issuecomment-2777146188> for a plan on how this
|
||||
could be improved.
|
||||
|
|
|
|||
|
|
@ -733,7 +733,7 @@ pub(crate) fn place_by_id<'db>(
|
|||
};
|
||||
|
||||
// If a symbol is undeclared, but qualified with `typing.Final`, we use the right-hand side
|
||||
// inferred type, without unioning with `Unknown`, because it can not be modified.
|
||||
// inferred type, without unioning with `Unknown`, because it cannot be modified.
|
||||
if let Some(qualifiers) = declared.is_bare_final() {
|
||||
let bindings = all_considered_bindings();
|
||||
return place_from_bindings_impl(db, bindings, requires_explicit_reexport)
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
//! of `test`. When evaluating a constraint, there are three possible outcomes: always true, always
|
||||
//! false, or ambiguous. For a simple constraint like this, always-true and always-false correspond
|
||||
//! to the case in which we can infer that the type of `test` is `Literal[True]` or `Literal[False]`.
|
||||
//! In any other case, like if the type of `test` is `bool` or `Unknown`, we can not statically
|
||||
//! In any other case, like if the type of `test` is `bool` or `Unknown`, we cannot statically
|
||||
//! determine whether `test` is truthy or falsy, so the outcome would be "ambiguous".
|
||||
//!
|
||||
//!
|
||||
|
|
@ -29,7 +29,7 @@
|
|||
//! Here, we would accumulate a reachability constraint of `test1 AND test2`. We can statically
|
||||
//! determine that this position is *always* reachable only if both `test1` and `test2` are
|
||||
//! always true. On the other hand, we can statically determine that this position is *never*
|
||||
//! reachable if *either* `test1` or `test2` is always false. In any other case, we can not
|
||||
//! reachable if *either* `test1` or `test2` is always false. In any other case, we cannot
|
||||
//! determine whether this position is reachable or not, so the outcome is "ambiguous". This
|
||||
//! corresponds to a ternary *AND* operation in [Kleene] logic:
|
||||
//!
|
||||
|
|
@ -60,7 +60,7 @@
|
|||
//! The third branch ends in a terminal statement [^1]. When we merge control flow, we need to consider
|
||||
//! the reachability through either the first or the second branch. The current position is only
|
||||
//! *definitely* unreachable if both `test1` and `test2` are always false. It is definitely
|
||||
//! reachable if *either* `test1` or `test2` is always true. In any other case, we can not statically
|
||||
//! reachable if *either* `test1` or `test2` is always true. In any other case, we cannot statically
|
||||
//! determine whether it is reachable or not. This operation corresponds to a ternary *OR* operation:
|
||||
//!
|
||||
//! ```text
|
||||
|
|
@ -91,7 +91,7 @@
|
|||
//! ## Explicit ambiguity
|
||||
//!
|
||||
//! In some cases, we explicitly record an “ambiguous” constraint. We do this when branching on
|
||||
//! something that we can not (or intentionally do not want to) analyze statically. `for` loops are
|
||||
//! something that we cannot (or intentionally do not want to) analyze statically. `for` loops are
|
||||
//! one example:
|
||||
//! ```py
|
||||
//! def _():
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ fn from_negative_i32(index: i32) -> usize {
|
|||
static_assertions::const_assert!(usize::BITS >= 32);
|
||||
|
||||
index.checked_neg().map(from_nonnegative_i32).unwrap_or({
|
||||
// 'checked_neg' only fails for i32::MIN. We can not
|
||||
// 'checked_neg' only fails for i32::MIN. We cannot
|
||||
// represent -i32::MIN as a i32, but we can represent
|
||||
// it as a usize, since usize is at least 32 bits.
|
||||
from_nonnegative_i32(i32::MAX) + 1
|
||||
|
|
|
|||
|
|
@ -297,7 +297,7 @@ impl AttributeKind {
|
|||
/// When invoked on a class object, the fallback type (a class attribute) can shadow a
|
||||
/// non-data descriptor of the meta-type (the class's metaclass). However, this is not
|
||||
/// true for instances. When invoked on an instance, the fallback type (an attribute on
|
||||
/// the instance) can not completely shadow a non-data descriptor of the meta-type (the
|
||||
/// the instance) cannot completely shadow a non-data descriptor of the meta-type (the
|
||||
/// class), because we do not currently attempt to statically infer if an instance
|
||||
/// attribute is definitely defined (i.e. to check whether a particular method has been
|
||||
/// called).
|
||||
|
|
@ -4412,7 +4412,7 @@ impl<'db> Type<'db> {
|
|||
};
|
||||
|
||||
if result.is_class_var() && self.is_typed_dict() {
|
||||
// `ClassVar`s on `TypedDictFallback` can not be accessed on inhabitants of `SomeTypedDict`.
|
||||
// `ClassVar`s on `TypedDictFallback` cannot be accessed on inhabitants of `SomeTypedDict`.
|
||||
// They can only be accessed on `SomeTypedDict` directly.
|
||||
return Place::Undefined.into();
|
||||
}
|
||||
|
|
@ -12050,7 +12050,7 @@ pub(crate) mod tests {
|
|||
assert!(todo1.is_assignable_to(&db, int));
|
||||
|
||||
// We lose information when combining several `Todo` types. This is an
|
||||
// acknowledged limitation of the current implementation. We can not
|
||||
// acknowledged limitation of the current implementation. We cannot
|
||||
// easily store the meta information of several `Todo`s in a single
|
||||
// variant, as `TodoType` needs to implement `Copy`, meaning it can't
|
||||
// contain `Vec`/`Box`/etc., and can't be boxed itself.
|
||||
|
|
|
|||
|
|
@ -2000,7 +2000,7 @@ pub(super) fn report_slice_step_size_zero(context: &InferContext, node: AnyNodeR
|
|||
let Some(builder) = context.report_lint(&ZERO_STEPSIZE_IN_SLICE, node) else {
|
||||
return;
|
||||
};
|
||||
builder.into_diagnostic("Slice step size can not be zero");
|
||||
builder.into_diagnostic("Slice step size cannot be zero");
|
||||
}
|
||||
|
||||
fn report_invalid_assignment_with_message(
|
||||
|
|
|
|||
|
|
@ -714,7 +714,7 @@ fn is_subtype_in_invariant_position<'db>(
|
|||
// TODO:
|
||||
// This should be removed and properly handled in the respective
|
||||
// `(Type::TypeVar(_), _) | (_, Type::TypeVar(_))` branch of
|
||||
// `Type::has_relation_to_impl`. Right now, we can not generally
|
||||
// `Type::has_relation_to_impl`. Right now, we cannot generally
|
||||
// return `ConstraintSet::from(true)` from that branch, as that
|
||||
// leads to union simplification, which means that we lose track
|
||||
// of type variables without recording the constraints under which
|
||||
|
|
|
|||
|
|
@ -3804,7 +3804,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
|
||||
let msg = if !member_exists {
|
||||
format!(
|
||||
"Can not assign to unresolved attribute `{attribute}` on type `{}`",
|
||||
"Cannot assign to unresolved attribute `{attribute}` on type `{}`",
|
||||
object_ty.display(db)
|
||||
)
|
||||
} else if is_setattr_synthesized {
|
||||
|
|
@ -3840,7 +3840,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
|||
self.context.report_lint(&UNRESOLVED_ATTRIBUTE, target)
|
||||
{
|
||||
builder.into_diagnostic(format_args!(
|
||||
"Can not assign object of type `{}` to attribute \
|
||||
"Cannot assign object of type `{}` to attribute \
|
||||
`{attribute}` on type `{}` with \
|
||||
custom `__setattr__` method.",
|
||||
value_ty.display(db),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue