Jeremiah Lowin commited on
Commit
8a6378e
·
1 Parent(s): 3dafe0d

Fix tests

Browse files
src/fastmcp/resources/types.py CHANGED
@@ -8,7 +8,7 @@ from typing import Any, Callable, Union
8
  import httpx
9
  import pydantic.json
10
  import pydantic_core
11
- from pydantic import Field
12
 
13
  from fastmcp.resources.base import Resource
14
 
@@ -91,6 +91,15 @@ class FileResource(Resource):
91
  raise ValueError("Path must be absolute")
92
  return path
93
 
 
 
 
 
 
 
 
 
 
94
  async def read(self) -> Union[str, bytes]:
95
  """Read the file content."""
96
  try:
 
8
  import httpx
9
  import pydantic.json
10
  import pydantic_core
11
+ from pydantic import Field, ValidationInfo
12
 
13
  from fastmcp.resources.base import Resource
14
 
 
91
  raise ValueError("Path must be absolute")
92
  return path
93
 
94
+ @pydantic.field_validator("is_binary")
95
+ @classmethod
96
+ def set_binary_from_mime_type(cls, is_binary: bool, info: ValidationInfo) -> bool:
97
+ """Set is_binary based on mime_type if not explicitly set."""
98
+ if is_binary:
99
+ return True
100
+ mime_type = info.data.get("mime_type", "text/plain")
101
+ return not mime_type.startswith("text/")
102
+
103
  async def read(self) -> Union[str, bytes]:
104
  """Read the file content."""
105
  try:
tests/resources/test_resource_template.py CHANGED
@@ -22,7 +22,8 @@ class TestResourceTemplate:
22
  assert template.uri_template == "test://{key}/{value}"
23
  assert template.name == "test"
24
  assert template.mime_type == "text/plain" # default
25
- assert template.fn == my_func
 
26
 
27
  def test_template_matches(self):
28
  """Test matching URIs against a template."""
 
22
  assert template.uri_template == "test://{key}/{value}"
23
  assert template.name == "test"
24
  assert template.mime_type == "text/plain" # default
25
+ test_input = {"key": "test", "value": 42}
26
+ assert template.fn(**test_input) == my_func(**test_input)
27
 
28
  def test_template_matches(self):
29
  """Test matching URIs against a template."""