mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
Relax constraints on pep8-naming module validation (#3043)
This commit is contained in:
parent
c297d46899
commit
b39f960cd1
11 changed files with 59 additions and 28 deletions
|
@ -22,3 +22,28 @@ pub fn is_identifier(s: &str) -> bool {
|
|||
pub fn is_mangled_private(id: &str) -> bool {
|
||||
id.starts_with("__") && !id.ends_with("__")
|
||||
}
|
||||
|
||||
/// Returns `true` if a string is a PEP 8-compliant module name (i.e., consists of lowercase
|
||||
/// letters, numbers, and underscores).
|
||||
pub fn is_module_name(s: &str) -> bool {
|
||||
s.chars()
|
||||
.all(|c| c.is_lowercase() || c.is_numeric() || c == '_')
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::identifiers::is_module_name;
|
||||
|
||||
#[test]
|
||||
fn test_is_module_name() {
|
||||
assert!(is_module_name("a"));
|
||||
assert!(is_module_name("abc"));
|
||||
assert!(is_module_name("abc0"));
|
||||
assert!(is_module_name("abc_"));
|
||||
assert!(is_module_name("a_b_c"));
|
||||
assert!(is_module_name("0abc"));
|
||||
assert!(is_module_name("_abc"));
|
||||
assert!(!is_module_name("a-b-c"));
|
||||
assert!(!is_module_name("a_B_c"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@ use regex::Regex;
|
|||
|
||||
pub static STRING_QUOTE_PREFIX_REGEX: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r#"^(?i)[urb]*['"](?P<raw>.*)['"]$"#).unwrap());
|
||||
pub static LOWER_OR_UNDERSCORE: Lazy<Regex> =
|
||||
Lazy::new(|| Regex::new(r"^[a-z][a-z0-9_]*$").unwrap());
|
||||
|
||||
pub fn is_lower(s: &str) -> bool {
|
||||
let mut cased = false;
|
||||
|
@ -30,11 +28,6 @@ pub fn is_upper(s: &str) -> bool {
|
|||
cased
|
||||
}
|
||||
|
||||
// Module names should be lowercase, and may contain underscore
|
||||
pub fn is_lower_with_underscore(s: &str) -> bool {
|
||||
LOWER_OR_UNDERSCORE.is_match(s)
|
||||
}
|
||||
|
||||
/// Remove prefixes (u, r, b) and quotes around a string. This expects the given
|
||||
/// string to be a valid Python string representation, it doesn't do any
|
||||
/// validation.
|
||||
|
@ -50,7 +43,7 @@ pub fn strip_quotes_and_prefixes(s: &str) -> &str {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::string::{is_lower, is_lower_with_underscore, is_upper, strip_quotes_and_prefixes};
|
||||
use crate::string::{is_lower, is_upper, strip_quotes_and_prefixes};
|
||||
|
||||
#[test]
|
||||
fn test_is_lower() {
|
||||
|
@ -63,19 +56,6 @@ mod tests {
|
|||
assert!(!is_lower("_"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_lower_underscore() {
|
||||
assert!(is_lower_with_underscore("a"));
|
||||
assert!(is_lower_with_underscore("abc"));
|
||||
assert!(is_lower_with_underscore("abc0"));
|
||||
assert!(is_lower_with_underscore("abc_"));
|
||||
assert!(is_lower_with_underscore("a_b_c"));
|
||||
assert!(!is_lower_with_underscore("a-b-c"));
|
||||
assert!(!is_lower_with_underscore("a_B_c"));
|
||||
assert!(!is_lower_with_underscore("0abc"));
|
||||
assert!(!is_lower_with_underscore("_abc"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_upper() {
|
||||
assert!(is_upper("ABC"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue