- 
  Notifications
 You must be signed in to change notification settings 
- Fork 311
 feat: add join method to Url class
 #1378
 
 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
Changes from all commits
a7d9351
 7ef57ba
 e8bd322
 8b70975
 6a4fa06
 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -155,6 +155,14 @@ impl PyUrl { | |
| (self.__str__(),) | ||
| } | ||
|  | ||
| fn __truediv__(&self, other: &str) -> PyResult<Self> { | ||
| self.join(other, true) | ||
| } | ||
|  | ||
| fn __floordiv__(&self, other: &str) -> PyResult<Self> { | ||
| self.join(other, false) | ||
| } | ||
| 
 Comment on lines
 
 +158
  to 
 +164
 
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, sorry I missed these in the last round of review. I think the difference between the  I think better we just have  
 Suggested change
   
  fn __truediv__(&self, other: &str) -> PyResult<Self> {
   
  self.join(other, true)
   
  }
   
  fn __floordiv__(&self, other: &str) -> PyResult<Self> {
   
  self.join(other, false)
   
  }
   
  fn __truediv__(&self, other: &str) -> PyResult<Self> {
   
  self.join(other, false)
   
  }
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay  a = Url("http://a") print(a / "b" / "c" / "d") # http://a/b/c/d/ a = Url("file:///home/user/") print(a / "music" / "pop") # file:///home/user/music/pop/ With  print(a / "dir" / "dir" / "dir" // "file.txt") # file:///home/user/dir/dir/dir/file.txt There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. Yikes, there are so many subleties here! It seems to me that our  >>> urllib.parse.urljoin("https://foo.com/a", "b") 'https://foo.com/b' versus pathlib's >>> pathlib.Path("/foo/a").joinpath("b") PosixPath('/foo/a/b') Given these are inconsistent, I think we should perhaps back away from trying to have pathlib-like semantics at all. Would you be open to the idea of dropping the operators from the PR completely, so we can get  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively we could also have  And then could have  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great question. I think I'd prefer we just had  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think without comment from anyone else, let's just do  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be great to have more time to discuss the semantics (does it need to match  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
 Really sorry for the late response. The main URL joining part is handled by rust-url's join method which implements the WHATWG URL spec. So, the  
 I think the  IMO, we can have  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Viicos @davidhewitt  | ||
|  | ||
| #[classmethod] | ||
| #[pyo3(signature=(*, scheme, host, username=None, password=None, port=None, path=None, query=None, fragment=None))] | ||
| #[allow(clippy::too_many_arguments)] | ||
|  | @@ -190,6 +198,27 @@ impl PyUrl { | |
| } | ||
| cls.call1((url,)) | ||
| } | ||
|  | ||
| #[pyo3(signature=(path, append_trailing_slash=false))] | ||
| pub fn join(&self, path: &str, append_trailing_slash: bool) -> PyResult<Self> { | ||
| let mut new_url = self | ||
| .lib_url | ||
| .join(path) | ||
| .map_err(|err| PyValueError::new_err(err.to_string()))?; | ||
|  | ||
| if append_trailing_slash && !(new_url.query().is_some() || new_url.fragment().is_some()) { | ||
| let path_segments_result = new_url.path_segments_mut().map(|mut segments| { | ||
| segments.pop_if_empty().push(""); | ||
| }); | ||
|  | ||
| if path_segments_result.is_err() { | ||
| let mut new_path = new_url.path().to_string(); | ||
| new_path.push('/'); | ||
| new_url.set_path(&new_path); | ||
| } | ||
| } | ||
|  
 davidhewitt marked this conversation as resolved.
 Show resolved
 Hide resolved | ||
| Ok(PyUrl::new(new_url)) | ||
| } | ||
| } | ||
|  | ||
| #[pyclass(name = "MultiHostUrl", module = "pydantic_core._pydantic_core", subclass, frozen)] | ||
|  | ||