Skip to content
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

Fix deprecation warnings #1013

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

def validate_environment(specification):
try:
specification = schema.CondaSpecification.parse_obj(specification)
specification = schema.CondaSpecification.model_validate(specification)
return True
except pydantic.ValidationError:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def lock_environment(
lockfile_filename = pathlib.Path.cwd() / "conda-lock.yaml"

with environment_filename.open("w") as f:
json.dump(spec.dict(), f)
json.dump(spec.model_dump(), f)

context.log.info(
"Note that the output of `conda config --show` displayed below only reflects "
Expand Down
13 changes: 8 additions & 5 deletions conda-store-server/conda_store_server/_internal/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class Settings(BaseModel):

# Conda Environment
class CondaSpecificationPip(BaseModel):
model_config = ConfigDict(from_attributes=True)
pip: List[PipArg] = []


Expand All @@ -370,11 +371,12 @@ class CondaSpecification(BaseModel):
name: Annotated[str, StringConstraints(pattern=f"^[{ALLOWED_CHARACTERS}]+$")]
prefix: Optional[str] = None
variables: Optional[Dict[str, Union[str, int]]] = None
model_config = ConfigDict(from_attributes=True)

@classmethod
def parse_obj(cls, specification):
def model_validate(cls, specification):
try:
return super().parse_obj(specification)
return super().model_validate(specification)
except ValidationError as e:
# there can be multiple errors. Let's build a comprehensive summary
# to return to the end user.
Expand Down Expand Up @@ -412,9 +414,10 @@ class LockfileSpecification(BaseModel):
name: Annotated[str, StringConstraints(pattern=f"^[{ALLOWED_CHARACTERS}]+$")] # noqa: F722
description: Optional[str] = ""
lockfile: Lockfile
model_config = ConfigDict(from_attributes=True)

@classmethod
def parse_obj(cls, specification):
def model_validate(cls, specification):
# To show a human-readable error if no data is provided
specification = {} if specification is None else specification
# This uses pop because the version field must not be part of Lockfile
Expand All @@ -429,7 +432,7 @@ def parse_obj(cls, specification):
"Expected lockfile to have no version field, or version=1",
)

return super().parse_obj(specification)
return super().model_validate(specification)

def model_dump(self):
res = super().model_dump()
Expand All @@ -443,7 +446,7 @@ def model_dump(self):
def __str__(self):
# This makes sure the format is suitable for output if this object is
# converted to a string, which can also happen implicitly
return str(self.dict())
return str(self.model_dump())


###############################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ def paginated_api_response(
)
return {
"status": "ok",
"data": [object_schema.from_orm(_).dict(exclude=exclude) for _ in query.all()],
"data": [
object_schema.model_validate(_).model_dump(exclude=exclude)
for _ in query.all()
],
"page": (paginated_args["offset"] // paginated_args["limit"]) + 1,
"size": paginated_args["limit"],
"count": count,
Expand Down Expand Up @@ -300,7 +303,7 @@ async def api_get_namespace(

return {
"status": "ok",
"data": schema.Namespace.from_orm(namespace).model_dump(),
"data": schema.Namespace.model_validate(namespace).model_dump(),
}


Expand Down Expand Up @@ -682,7 +685,9 @@ async def api_list_environments(
if jwt:
# Fetch the environments visible to the supplied token
role_bindings = auth.entity_bindings(
AuthenticationToken.parse_obj(auth.authentication.decrypt_token(jwt))
AuthenticationToken.model_validate(
auth.authentication.decrypt_token(jwt)
)
)
else:
role_bindings = None
Expand Down Expand Up @@ -745,7 +750,7 @@ async def api_get_environment(

return {
"status": "ok",
"data": schema.Environment.from_orm(environment).dict(
"data": schema.Environment.model_validate(environment).model_dump(
exclude={"current_build"}
),
}
Expand Down Expand Up @@ -887,9 +892,11 @@ async def api_post_specification(
"description": environment_description,
"lockfile": specification,
}
specification = schema.LockfileSpecification.parse_obj(lockfile_spec)
specification = schema.LockfileSpecification.model_validate(
lockfile_spec
)
else:
specification = schema.CondaSpecification.parse_obj(specification)
specification = schema.CondaSpecification.model_validate(specification)
except yaml.error.YAMLError:
raise HTTPException(status_code=400, detail="Unable to parse. Invalid YAML")
except utils.CondaStoreError as e:
Expand Down Expand Up @@ -983,7 +990,7 @@ async def api_get_build(

return {
"status": "ok",
"data": schema.Build.from_orm(build).dict(exclude={"packages"}),
"data": schema.Build.model_validate(build).model_dump(exclude={"packages"}),
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def get_conda_store_ui(
"url_prefix": url_prefix,
"ui_prefix": router_conda_store_ui.prefix,
}
response = templates.TemplateResponse("conda-store-ui.html", context)
response = templates.TemplateResponse(request, "conda-store-ui.html", context)
if path.endswith("not-found"):
response.status_code = 404
return response
19 changes: 11 additions & 8 deletions conda-store-server/conda_store_server/_internal/server/views/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def sort_namespace(n):
"entity": entity,
}

return templates.TemplateResponse("create.html", context)
return templates.TemplateResponse(request, "create.html", context)


@router_ui.get("/")
Expand All @@ -74,7 +74,7 @@ async def ui_list_environments(
"platform": get_installer_platform(),
}

return templates.TemplateResponse("home.html", context)
return templates.TemplateResponse(request, "home.html", context)


@router_ui.get("/namespace/")
Expand All @@ -96,7 +96,7 @@ async def ui_list_namespaces(
"entity": entity,
}

return templates.TemplateResponse("namespace.html", context)
return templates.TemplateResponse(request, "namespace.html", context)


@router_ui.get("/environment/{namespace}/{environment_name}/")
Expand All @@ -122,6 +122,7 @@ async def ui_get_environment(
)
if environment is None:
return templates.TemplateResponse(
request,
"404.html",
{
"request": request,
Expand All @@ -145,7 +146,7 @@ async def ui_get_environment(
"spec": yaml.dump(spec),
}

return templates.TemplateResponse("environment.html", context)
return templates.TemplateResponse(request, "environment.html", context)


@router_ui.get("/environment/{namespace}/{environment_name}/edit/")
Expand All @@ -171,6 +172,7 @@ async def ui_edit_environment(
)
if environment is None:
return templates.TemplateResponse(
request,
"404.html",
{
"request": request,
Expand Down Expand Up @@ -198,7 +200,7 @@ async def ui_edit_environment(
"namespaces": [environment.namespace],
}

return templates.TemplateResponse("create.html", context)
return templates.TemplateResponse(request, "create.html", context)


@router_ui.get("/build/{build_id}/")
Expand All @@ -215,6 +217,7 @@ async def ui_get_build(
build = api.get_build(db, build_id)
if build is None:
return templates.TemplateResponse(
request,
"404.html",
{
"request": request,
Expand All @@ -239,7 +242,7 @@ async def ui_get_build(
"platform": get_installer_platform(),
}

return templates.TemplateResponse("build.html", context)
return templates.TemplateResponse(request, "build.html", context)


@router_ui.get("/user/")
Expand Down Expand Up @@ -276,7 +279,7 @@ async def ui_get_user(
"system_metrics": system_metrics,
"namespace_usage_metrics": namespace_usage_metrics,
}
return templates.TemplateResponse("user.html", context)
return templates.TemplateResponse(request, "user.html", context)


@router_ui.get("/setting/")
Expand Down Expand Up @@ -320,4 +323,4 @@ async def ui_get_setting(
db, namespace=namespace, environment_name=environment_name
),
}
return templates.TemplateResponse("setting.html", context)
return templates.TemplateResponse(request, "setting.html", context)
14 changes: 8 additions & 6 deletions conda-store-server/conda_store_server/_internal/worker/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def build_conda_environment(db: Session, conda_store, build):
with utils.timer(conda_store.log, f"building conda_prefix={conda_prefix}"):
if is_lockfile:
context = action.action_save_lockfile(
specification=schema.LockfileSpecification.parse_obj(
specification=schema.LockfileSpecification.model_validate(
build.specification.spec
),
stdout=LoggedStream(
Expand All @@ -227,7 +227,9 @@ def build_conda_environment(db: Session, conda_store, build):
else:
lock_backend, locker = conda_store.lock_plugin()
conda_lock_spec = locker.lock_environment(
spec=schema.CondaSpecification.parse_obj(build.specification.spec),
spec=schema.CondaSpecification.model_validate(
build.specification.spec
),
platforms=settings.conda_solve_platforms,
context=plugin_context.PluginContext(
conda_store=conda_store,
Expand Down Expand Up @@ -341,7 +343,7 @@ def solve_conda_environment(db: Session, conda_store, solve: orm.Solve):
_, locker = conda_store.lock_plugin()
conda_lock_spec = locker.lock_environment(
context=plugin_context.PluginContext(conda_store=conda_store),
spec=schema.CondaSpecification.parse_obj(solve.specification.spec),
spec=schema.CondaSpecification.model_validate(solve.specification.spec),
platforms=[conda_utils.conda_platform()],
)

Expand Down Expand Up @@ -440,7 +442,7 @@ def build_constructor_installer(db: Session, conda_store, build: orm.Build):
is_lockfile = build.specification.is_lockfile

if is_lockfile:
specification = schema.LockfileSpecification.parse_obj(
specification = schema.LockfileSpecification.model_validate(
build.specification.spec
)
else:
Expand All @@ -449,7 +451,7 @@ def build_constructor_installer(db: Session, conda_store, build: orm.Build):
# pinned dependencies. This code is wrapped into try/except
# because the lockfile lookup might fail if the file is not
# in external storage or on disk, or if parsing fails
specification = schema.LockfileSpecification.parse_obj(
specification = schema.LockfileSpecification.model_validate(
{
"name": build.specification.name,
"lockfile": json.loads(
Expand All @@ -463,7 +465,7 @@ def build_constructor_installer(db: Session, conda_store, build: orm.Build):
"Exception while obtaining lockfile, using specification",
exc_info=e,
)
specification = schema.CondaSpecification.parse_obj(
specification = schema.CondaSpecification.model_validate(
build.specification.spec
)

Expand Down
4 changes: 2 additions & 2 deletions conda-store-server/conda_store_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _check_build_key_version(self, proposal):

conda_solve_platforms = List(
[conda_utils.conda_platform()],
description="Conda platforms to solve environments for via conda-lock. Must include current platform.",
help="Conda platforms to solve environments for via conda-lock. Must include current platform.",
config=True,
)

Expand Down Expand Up @@ -651,7 +651,7 @@ def register_environment(
db=db,
conda_store=self,
namespace=namespace.name,
specification=schema.CondaSpecification.parse_obj(specification),
specification=schema.CondaSpecification.model_validate(specification),
)

spec_sha256 = utils.datastructure_hash(specification_model.model_dump())
Expand Down
3 changes: 2 additions & 1 deletion conda-store-server/conda_store_server/server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def authenticate(self, token):
authentication_token = self.predefined_tokens[token]
else:
authentication_token = self.decrypt_token(token)
return schema.AuthenticationToken.parse_obj(authentication_token)
return schema.AuthenticationToken.model_validate(authentication_token)
except Exception:
return None

Expand Down Expand Up @@ -521,6 +521,7 @@ def get_login_method(
):
request.session["next"] = next
return templates.TemplateResponse(
request,
"login.html",
{"request": request, "login_html": self.get_login_html(request, templates)},
)
Expand Down
2 changes: 1 addition & 1 deletion conda-store-server/tests/_internal/action/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_generate_conda_export(conda_store, conda_prefix):
# an environment is in an envs dir. See the discussion on PR #549.
context.result["name"] = "test-prefix"

schema.CondaSpecification.parse_obj(context.result)
schema.CondaSpecification.model_validate(context.result)


@pytest.mark.long_running_test
Expand Down
Loading
Loading