Skip to content

Commit

Permalink
Remove needless uses of PyString::intern (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt authored Nov 21, 2023
1 parent 3d3f406 commit a7739ec
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 19 deletions.
20 changes: 7 additions & 13 deletions src/lookup_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ impl fmt::Display for LookupKey {
}
}

macro_rules! py_string {
($py:ident, $str:expr) => {
PyString::intern($py, $str).into()
};
}

impl LookupKey {
pub fn from_py(py: Python, value: &PyAny, alt_alias: Option<&str>) -> PyResult<Self> {
if let Ok(alias_py) = value.downcast::<PyString>() {
Expand All @@ -67,7 +61,7 @@ impl LookupKey {
py_key1: alias_py.into_py(py),
path1: LookupPath::from_str(py, alias, Some(alias_py)),
key2: alt_alias.to_string(),
py_key2: py_string!(py, alt_alias),
py_key2: PyString::new(py, alt_alias).into(),
path2: LookupPath::from_str(py, alt_alias, None),
}),
None => Ok(Self::simple(py, alias, Some(alias_py))),
Expand Down Expand Up @@ -98,12 +92,12 @@ impl LookupKey {

fn simple(py: Python, key: &str, opt_py_key: Option<&PyString>) -> Self {
let py_key = match opt_py_key {
Some(py_key) => py_key.into_py(py),
None => py_string!(py, key),
Some(py_key) => py_key,
None => PyString::new(py, key),
};
Self::Simple {
key: key.to_string(),
py_key,
py_key: py_key.into(),
path: LookupPath::from_str(py, key, opt_py_key),
}
}
Expand Down Expand Up @@ -348,10 +342,10 @@ impl fmt::Display for LookupPath {
impl LookupPath {
fn from_str(py: Python, key: &str, py_key: Option<&PyString>) -> Self {
let py_key = match py_key {
Some(py_key) => py_key.into_py(py),
None => py_string!(py, key),
Some(py_key) => py_key,
None => PyString::new(py, key),
};
Self(vec![PathItem::S(key.to_string(), py_key)])
Self(vec![PathItem::S(key.to_string(), py_key.into())])
}

fn from_list(obj: &PyAny) -> PyResult<LookupPath> {
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl BuildSerializer for DataclassArgsBuilder {
let field_info: &PyDict = item.downcast()?;
let name: String = field_info.get_as_req(intern!(py, "name"))?;

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

if field_info.get_as(intern!(py, "serialization_exclude"))? == Some(true) {
fields.insert(name, SerField::new(py, key_py, None, None, true));
Expand Down
2 changes: 1 addition & 1 deletion src/validators/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl BuildValidator for ArgumentsValidator {
}
None => Some(LookupKey::from_string(py, &name)),
};
kwarg_key = Some(PyString::intern(py, &name).into());
kwarg_key = Some(PyString::new(py, &name).into());
}

let schema: &PyAny = arg.get_as_req(intern!(py, "schema"))?;
Expand Down
2 changes: 1 addition & 1 deletion src/validators/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl BuildValidator for DataclassValidator {
let validator = build_validator(sub_schema, config, definitions)?;

let post_init = if schema.get_as::<bool>(intern!(py, "post_init"))?.unwrap_or(false) {
Some(PyString::intern(py, "__post_init__").into_py(py))
Some(PyString::new(py, "__post_init__").into_py(py))
} else {
None
};
Expand Down
2 changes: 1 addition & 1 deletion src/validators/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl BuildValidator for ModelValidator {
class: class.into(),
post_init: schema
.get_as::<&str>(intern!(py, "post_init"))?
.map(|s| PyString::intern(py, s).into_py(py)),
.map(|s| PyString::new(py, s).into_py(py)),
frozen: schema.get_as(intern!(py, "frozen"))?.unwrap_or(false),
custom_init: schema.get_as(intern!(py, "custom_init"))?.unwrap_or(false),
root_model: schema.get_as(intern!(py, "root_model"))?.unwrap_or(false),
Expand Down
2 changes: 1 addition & 1 deletion src/validators/model_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl BuildValidator for ModelFieldsValidator {
fields.push(Field {
name: field_name.to_string(),
lookup_key,
name_py: PyString::intern(py, field_name).into(),
name_py: PyString::new(py, field_name).into(),
validator,
frozen: field_info.get_as::<bool>(intern!(py, "frozen"))?.unwrap_or(false),
});
Expand Down
2 changes: 1 addition & 1 deletion src/validators/typed_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl BuildValidator for TypedDictValidator {
fields.push(TypedDictField {
name: field_name.to_string(),
lookup_key,
name_py: PyString::intern(py, field_name).into(),
name_py: PyString::new(py, field_name).into(),
validator,
required,
});
Expand Down

0 comments on commit a7739ec

Please sign in to comment.