Skip to content

Commit b016313

Browse files
committed
Fix linting and pytest issues for types.py
Signed-off-by: Mihai Criveti <[email protected]>
1 parent 823f607 commit b016313

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

mcpgateway/types.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,61 @@ class Config:
479479

480480
# Root types
481481
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
487514
allowed_schemes = {"file"}
488515
host_required = False
489516

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+
490537

491538
class Root(BaseModel):
492539
"""A root directory or file.

0 commit comments

Comments
 (0)