@@ -479,14 +479,61 @@ class Config:
479
479
480
480
# Root types
481
481
class FileUrl (AnyUrl ):
482
- """A specialized URL for file resources.
483
-
484
- This URL accepts only the "file" scheme and does not require a host.
485
- """
486
-
482
+ """A specialized URL type for local file-scheme resources.
483
+
484
+ Key characteristics
485
+ -------------------
486
+ * Scheme restricted – only the "file" scheme is permitted
487
+ (e.g. file:///path/to/file.txt).
488
+ * No host required – "file" URLs typically omit a network host;
489
+ therefore, the host component is not mandatory.
490
+ * String-friendly equality – developers naturally expect
491
+ FileUrl("file:///data") == "file:///data" to evaluate True.
492
+ AnyUrl (Pydantic) does not implement that, so we override
493
+ __eq__ to compare against plain strings transparently.
494
+ Hash semantics are kept consistent by delegating to the parent class.
495
+
496
+ Examples
497
+ --------
498
+ >>> url = FileUrl("file:///etc/hosts")
499
+ >>> url.scheme
500
+ 'file'
501
+ >>> url == "file:///etc/hosts"
502
+ True
503
+ >>> {"path": url} # hashable
504
+ {'path': FileUrl('file:///etc/hosts')}
505
+
506
+ Notes
507
+ -----
508
+ The override does not interfere with comparisons to other
509
+ AnyUrl/FileUrl instances; those still use the superclass
510
+ implementation.
511
+ """
512
+
513
+ # Restrict to the "file" scheme and omit host requirement
487
514
allowed_schemes = {"file" }
488
515
host_required = False
489
516
517
+ def __eq__ (self , other ): # type: ignore[override]
518
+ """Return True when other is an equivalent URL or string.
519
+
520
+ If other is a str it is coerced with str(self) for comparison;
521
+ otherwise defer to AnyUrl's comparison.
522
+
523
+ Args:
524
+ other (Any): The object to compare against. May be a str, FileUrl, or AnyUrl.
525
+
526
+ Returns:
527
+ bool: True if the other value is equal to this URL, either as a string
528
+ or as another URL object. False otherwise.
529
+ """
530
+ if isinstance (other , str ):
531
+ return str (self ) == other
532
+ return super ().__eq__ (other )
533
+
534
+ # Keep hashing behaviour aligned with equality
535
+ __hash__ = AnyUrl .__hash__
536
+
490
537
491
538
class Root (BaseModel ):
492
539
"""A root directory or file.
0 commit comments