-
Notifications
You must be signed in to change notification settings - Fork 290
Add config setting to avoid adding trailing slash to URLs #1719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #1719 will not alter performanceComparing Summary
|
Bound(Bound<'py, PyUrl>), | ||
Owned(PyUrl), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidhewitt is there a better way of doing this, or a better name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of this is to avoid allocating the Python object until it's absolutely needed. Maybe call it LazyUrlObject
and have the variants be
Bound(Bound<'py, PyUrl>), | |
Owned(PyUrl), | |
Object(Bound<'py, PyUrl>), | |
Struct(PyUrl), |
impl From<PyUrl> for Url { | ||
fn from(value: PyUrl) -> Url { | ||
value.lib_url | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyUrl { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly query
#[getter]
also needs this treatment?
Bound(Bound<'py, PyUrl>), | ||
Owned(PyUrl), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of this is to avoid allocating the Python object until it's absolutely needed. Maybe call it LazyUrlObject
and have the variants be
Bound(Bound<'py, PyUrl>), | |
Owned(PyUrl), | |
Object(Bound<'py, PyUrl>), | |
Struct(PyUrl), |
EitherUrl::Py(py_url) => Ok(py_url), | ||
EitherUrl::Rust(rust_url) => Bound::new(py, PyUrl::new(rust_url)), | ||
EitherUrl::Bound(py_url) => Ok(py_url), | ||
EitherUrl::Owned(py_url) => Bound::new(py, py_url), | ||
} | ||
} | ||
} | ||
|
||
impl CopyFromPyUrl for EitherUrl<'_> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing this again, this looks like it probably should have been AsRef<Url>
and AsMut<Url>
implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyUrl.path
will still contain the trailing slash:
u = Url('https://example.com', add_trailing_slash=False)
u.path
#> '/'
Do we also want to remove it here?
if self.remove_trailing_slash && s.ends_with('/') { | ||
s = &s[..s.len() - 1]; | ||
} | ||
s | ||
} | ||
|
||
pub fn __repr__(&self) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does __repr__()
also need to be updated?
@@ -490,7 +514,11 @@ fn unicode_url(lib_url: &Url) -> String { | |||
s | |||
} | |||
_ => s, | |||
}; | |||
if remove_trailing_slash && s.ends_with('/') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the point is to preserve the input, not always add or always strip. Can we keep track of the input ending with a trailing slash?
If not the config option is confusing: it should be remove_trailing_slash: bool = False
to match the implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that it is working as expected but I'm not entirely sure why:
u = Url('https://example.com/', add_trailing_slash=False)
str(u)
#> 'https://example.com/'
Preparation for fixing pydantic/pydantic#7186