Skip to content

Commit a7739ec

Browse files
authored
Remove needless uses of PyString::intern (#1088)
1 parent 3d3f406 commit a7739ec

File tree

7 files changed

+13
-19
lines changed

7 files changed

+13
-19
lines changed

src/lookup_key.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ impl fmt::Display for LookupKey {
5151
}
5252
}
5353

54-
macro_rules! py_string {
55-
($py:ident, $str:expr) => {
56-
PyString::intern($py, $str).into()
57-
};
58-
}
59-
6054
impl LookupKey {
6155
pub fn from_py(py: Python, value: &PyAny, alt_alias: Option<&str>) -> PyResult<Self> {
6256
if let Ok(alias_py) = value.downcast::<PyString>() {
@@ -67,7 +61,7 @@ impl LookupKey {
6761
py_key1: alias_py.into_py(py),
6862
path1: LookupPath::from_str(py, alias, Some(alias_py)),
6963
key2: alt_alias.to_string(),
70-
py_key2: py_string!(py, alt_alias),
64+
py_key2: PyString::new(py, alt_alias).into(),
7165
path2: LookupPath::from_str(py, alt_alias, None),
7266
}),
7367
None => Ok(Self::simple(py, alias, Some(alias_py))),
@@ -98,12 +92,12 @@ impl LookupKey {
9892

9993
fn simple(py: Python, key: &str, opt_py_key: Option<&PyString>) -> Self {
10094
let py_key = match opt_py_key {
101-
Some(py_key) => py_key.into_py(py),
102-
None => py_string!(py, key),
95+
Some(py_key) => py_key,
96+
None => PyString::new(py, key),
10397
};
10498
Self::Simple {
10599
key: key.to_string(),
106-
py_key,
100+
py_key: py_key.into(),
107101
path: LookupPath::from_str(py, key, opt_py_key),
108102
}
109103
}
@@ -348,10 +342,10 @@ impl fmt::Display for LookupPath {
348342
impl LookupPath {
349343
fn from_str(py: Python, key: &str, py_key: Option<&PyString>) -> Self {
350344
let py_key = match py_key {
351-
Some(py_key) => py_key.into_py(py),
352-
None => py_string!(py, key),
345+
Some(py_key) => py_key,
346+
None => PyString::new(py, key),
353347
};
354-
Self(vec![PathItem::S(key.to_string(), py_key)])
348+
Self(vec![PathItem::S(key.to_string(), py_key.into())])
355349
}
356350

357351
fn from_list(obj: &PyAny) -> PyResult<LookupPath> {

src/serializers/type_serializers/dataclass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl BuildSerializer for DataclassArgsBuilder {
3939
let field_info: &PyDict = item.downcast()?;
4040
let name: String = field_info.get_as_req(intern!(py, "name"))?;
4141

42-
let key_py: Py<PyString> = PyString::intern(py, &name).into_py(py);
42+
let key_py: Py<PyString> = PyString::new(py, &name).into_py(py);
4343

4444
if field_info.get_as(intern!(py, "serialization_exclude"))? == Some(true) {
4545
fields.insert(name, SerField::new(py, key_py, None, None, true));

src/validators/arguments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl BuildValidator for ArgumentsValidator {
7373
}
7474
None => Some(LookupKey::from_string(py, &name)),
7575
};
76-
kwarg_key = Some(PyString::intern(py, &name).into());
76+
kwarg_key = Some(PyString::new(py, &name).into());
7777
}
7878

7979
let schema: &PyAny = arg.get_as_req(intern!(py, "schema"))?;

src/validators/dataclass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl BuildValidator for DataclassValidator {
477477
let validator = build_validator(sub_schema, config, definitions)?;
478478

479479
let post_init = if schema.get_as::<bool>(intern!(py, "post_init"))?.unwrap_or(false) {
480-
Some(PyString::intern(py, "__post_init__").into_py(py))
480+
Some(PyString::new(py, "__post_init__").into_py(py))
481481
} else {
482482
None
483483
};

src/validators/model.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl BuildValidator for ModelValidator {
8989
class: class.into(),
9090
post_init: schema
9191
.get_as::<&str>(intern!(py, "post_init"))?
92-
.map(|s| PyString::intern(py, s).into_py(py)),
92+
.map(|s| PyString::new(py, s).into_py(py)),
9393
frozen: schema.get_as(intern!(py, "frozen"))?.unwrap_or(false),
9494
custom_init: schema.get_as(intern!(py, "custom_init"))?.unwrap_or(false),
9595
root_model: schema.get_as(intern!(py, "root_model"))?.unwrap_or(false),

src/validators/model_fields.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl BuildValidator for ModelFieldsValidator {
9393
fields.push(Field {
9494
name: field_name.to_string(),
9595
lookup_key,
96-
name_py: PyString::intern(py, field_name).into(),
96+
name_py: PyString::new(py, field_name).into(),
9797
validator,
9898
frozen: field_info.get_as::<bool>(intern!(py, "frozen"))?.unwrap_or(false),
9999
});

src/validators/typed_dict.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl BuildValidator for TypedDictValidator {
120120
fields.push(TypedDictField {
121121
name: field_name.to_string(),
122122
lookup_key,
123-
name_py: PyString::intern(py, field_name).into(),
123+
name_py: PyString::new(py, field_name).into(),
124124
validator,
125125
required,
126126
});

0 commit comments

Comments
 (0)