Skip to content

Commit

Permalink
Allow Hashable = None in type annotations (#9442)
Browse files Browse the repository at this point in the history
Closes #9441.
  • Loading branch information
charliermarsh authored Jan 9, 2024
1 parent 86b1ae9 commit 20af5a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/ruff/RUF013_0.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing
from typing import Annotated, Any, Literal, Optional, Tuple, Union
from typing import Annotated, Any, Literal, Optional, Tuple, Union, Hashable


def f(arg: int):
Expand Down Expand Up @@ -257,3 +257,13 @@ def f(arg: "Text" = None):

def f(arg: MaybeInt = None):
pass


# Hashable

def f(arg: Hashable = None): # OK
pass


def f(arg: Hashable | int = None): # OK
pass
9 changes: 9 additions & 0 deletions crates/ruff_linter/src/rules/ruff/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ enum TypingTarget<'a> {
/// A `typing.Annotated` type e.g., `Annotated[int, ...]`.
Annotated(&'a Expr),

/// The `typing.Hashable` type.
Hashable,

/// Special type used to represent an unknown type (and not a typing target)
/// which could be a type alias.
Unknown,
Expand Down Expand Up @@ -121,6 +124,10 @@ impl<'a> TypingTarget<'a> {
Some(TypingTarget::Any)
} else if matches!(call_path.as_slice(), ["" | "builtins", "object"]) {
Some(TypingTarget::Object)
} else if semantic.match_typing_call_path(&call_path, "Hashable")
|| matches!(call_path.as_slice(), ["collections", "abc", "Hashable"])
{
Some(TypingTarget::Hashable)
} else if !is_known_type(&call_path, minor_version) {
// If it's not a known type, we assume it's `Any`.
Some(TypingTarget::Unknown)
Expand All @@ -142,6 +149,7 @@ impl<'a> TypingTarget<'a> {
match self {
TypingTarget::None
| TypingTarget::Optional(_)
| TypingTarget::Hashable
| TypingTarget::Any
| TypingTarget::Object
| TypingTarget::Unknown => true,
Expand Down Expand Up @@ -191,6 +199,7 @@ impl<'a> TypingTarget<'a> {
// `Literal` cannot contain `Any` as it's a dynamic value.
TypingTarget::Literal(_)
| TypingTarget::None
| TypingTarget::Hashable
| TypingTarget::Object
| TypingTarget::Known
| TypingTarget::Unknown => false,
Expand Down

0 comments on commit 20af5a7

Please sign in to comment.