Skip to content

Commit

Permalink
Convert requirementslib to pydantic (#368)
Browse files Browse the repository at this point in the history
* Convert requirementslib to pydantic

* Should never import setuptools, causes bad side effects.

* Fix import

* Remove tests of removed functions.

* Fix import

* Fix import

* Fix import

* Fix to use isinstance

* Fix import

* Fix import

* Remove test that tests removed method

* Remove tests of removed  methods.

* fix test

* Remove tests of removed  methods.

* Remove tests of removed  methods.

* Fix python markers.

* Revert "Remove tests of removed  methods."

This reverts commit 73fb203.

* fix tests

* Update test case to avoid antiquated package.

* Check pt tests

* Fix edge case of VCS requirements and expected resulting dictionary.

* Fix more tests

* more test fixes

* down to 6 failing tests

* down to 5 failing tests

* Fix tests, down to 4 failing

* Fix tests, down to 3 failing

* Do not force parse of ireq when it may not be parsable.

* latest changes that work for both pipenv and requirementslib.

* latest changes that work for both pipenv and requirementslib.

* remove test that is problematic in github acttions.

* remove test that is problematic in github acttions.

* Apply fix to failing test.

* PR feedback

* Remove unused type imports

---------

Co-authored-by: Oz Tiram <oz.tiram@gmail.com>
  • Loading branch information
matteius and oz123 committed Jun 17, 2023
1 parent 400781f commit c17411d
Show file tree
Hide file tree
Showing 24 changed files with 1,674 additions and 3,765 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ classifier =
zip_safe = true
python_requires = >=3.7
install_requires =
attrs>=19.2
distlib>=0.2.8
pep517>=0.5.0
pip>=23.1
platformdirs
plette[validation]
pydantic
requests
setuptools>=40.8
tomlkit>=0.5.3
Expand Down
25 changes: 7 additions & 18 deletions src/requirementslib/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,11 @@ def get_long_path(short_path: Text) -> Text:
return buffer.value


def normalize_path(path):
"""Return a case-normalized absolute variable-expanded path.
:param str path: The non-normalized path
:return: A normalized, expanded, case-normalized path
:rtype: str
"""

path = os.path.abspath(os.path.expandvars(os.path.expanduser(str(path))))
if os.name == "nt" and os.path.exists(path):

path = get_long_path(path)

return os.path.normpath(os.path.normcase(path))
def normalize_path(path: str) -> str:
"""Return a case-normalized absolute variable-expanded path."""
return os.path.expandvars(
os.path.expanduser(os.path.normcase(os.path.normpath(os.path.abspath(str(path)))))
)


def normalize_drive(path):
Expand All @@ -114,10 +105,8 @@ def normalize_drive(path):
identified with either upper or lower cased drive names. The case is
always converted to uppercase because it seems to be preferred.
"""
if os.name != "nt" or not (
isinstance(path, str) or getattr(path, "__fspath__", None)
):
return path # type: ignore
if os.name != "nt" or not isinstance(path, str):
return path

drive, tail = os.path.splitdrive(path)
# Only match (lower cased) local drives (e.g. 'c:'), not UNC mounts.
Expand Down
167 changes: 0 additions & 167 deletions src/requirementslib/models/cache.py

This file was deleted.

28 changes: 28 additions & 0 deletions src/requirementslib/models/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Any, Dict

from pydantic import BaseModel, Extra


class ReqLibBaseModel(BaseModel):
def __setattr__(self, name, value): # noqa: C901 (ignore complexity)
private_attributes = {
field_name
for field_name in self.__annotations__
if field_name.startswith("_")
}

if name in private_attributes or name in self.__fields__:
return object.__setattr__(self, name, value)

if self.__config__.extra is not Extra.allow and name not in self.__fields__:
raise ValueError(f'"{self.__class__.__name__}" object has no field "{name}"')

object.__setattr__(self, name, value)

def dict(self, *args, **kwargs) -> Dict[str, Any]:
"""The requirementslib classes make use of a lot of private attributes
which do not get serialized out to the dict by default in pydantic."""
model_dict = super().dict(*args, **kwargs)
private_attrs = {k: v for k, v in self.__dict__.items() if k.startswith("_")}
model_dict.update(private_attrs)
return model_dict

0 comments on commit c17411d

Please sign in to comment.