Requirement class final

Requirement

python docstring

Parse a requirement.

Parse a given requirement string into its parts, such as name, specifier, URL, and extras. Raises InvalidRequirement on a badly-formed requirement string.

python source

class Requirement:
    """Parse a requirement.

    Parse a given requirement string into its parts, such as name, specifier,
    URL, and extras. Raises InvalidRequirement on a badly-formed requirement
    string.
    """

    # TODO: Can we test whether something is contained within a requirement?
    #       If so how do we do that? Do we need to test against the _name_ of
    #       the thing as well as the version? What about the markers?
    # TODO: Can we normalize the name and extra name?

    def __init__(self, requirement_string: str) -> None:
        try:
            parsed = _parse_requirement(requirement_string)
        except ParserSyntaxError as e:
            raise InvalidRequirement(str(e)) from e

        self.name: str = parsed.name
        if parsed.url:
            parsed_url = urllib.parse.urlparse(parsed.url)
            if parsed_url.scheme == "file":
                if urllib.parse.urlunparse(parsed_url) != parsed.url:
                    raise InvalidRequirement("Invalid URL given")
            elif not (parsed_url.scheme and parsed_url.netloc) or (
                not parsed_url.scheme and not parsed_url.netloc
            ):
                raise InvalidRequirement(f"Invalid URL: {parsed.url}")
            self.url: Optional[str] = parsed.url
        else:
            self.url = None
        self.extras: Set[str] = set(parsed.extras if parsed.extras else [])
        self.specifier: SpecifierSet = SpecifierSet(parsed.specifier)
        self.marker: Optional[Marker] = None
        if parsed.marker is not None:
            self.marker = Marker.__new__(Marker)
            self.marker._markers = _normalize_extra_values(parsed.marker)

    def __str__(self) -> str:
        parts: List[str] = [self.name]

        if self.extras:
            formatted_extras = ",".join(sorted(self.extras))
            parts.append(f"[{formatted_extras}]")

        if self.specifier:
            parts.append(str(self.specifier))

        if self.url:
            parts.append(f"@ {self.url}")
            if self.marker:
                parts.append(" ")

        if self.marker:
            parts.append(f"; {self.marker}")

        return "".join(parts)

    def __repr__(self) -> str:
        return f"<Requirement('{self}')>"

    def __hash__(self) -> int:
        return hash((self.__class__.__name__, str(self)))

    def __eq__(self, other: Any) -> bool:
        if not isinstance(other, Requirement):
            return NotImplemented

        return (
            self.name == other.name
            and self.extras == other.extras
            and self.specifier == other.specifier
            and self.url == other.url
            and self.marker == other.marker
        )
Inheritance
  • Object
  • PythonObjectInterface<PythonFfiDelegate<Object?>, Object?>
  • PythonObject
  • PythonClass
  • Requirement

Constructors

Requirement({required String requirement_string})
factory
Requirement.from(PythonClassInterface<PythonFfiDelegate<Object?>, Object?> pythonClass)

Properties

extras Object?
extras (getter)
getter/setter pair
finalizer Finalizer<(PythonFfiDelegate<Object?>, Object?)>
The finalizer for the python object. Gets invoked when the object is no longer accessible to the program.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
initializer → Initializer<PythonFfiDelegate<Object?>, Object?>
The initializer for the python object. Gets invoked at the start of the constructor.
finalinherited
marker Object?
marker (getter)
getter/setter pair
name Object?
name (getter)
getter/setter pair
platform → PythonFfiDelegate<Object?>
Gets the platform that this object is associated with.
no setterinherited
reference Object?
Gets the reference to the python object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
specifier Object?
specifier (getter)
getter/setter pair
url Object?
url (getter)
getter/setter pair

Methods

debugDump() → void
Dumps the python object to the console.
inherited
getAttribute<T extends Object?>(String attributeName) → T
Gets the attribute with the given name.
inherited
getAttributeOrNull<T extends Object?>(String attributeName) → T?
Gets the attribute with the given name or null if it does not exist.
inherited
getAttributeRaw<T extends PythonObjectInterface<PythonFfiDelegate<Object?>, Object?>>(String attributeName) → T
Gets the attribute with the given name.
inherited
getFunction(String name) → PythonFunctionInterface<PythonFfiDelegate<Object?>, Object?>
Gets the function with the given name.
inherited
getMethod(String functionName) → PythonFunctionInterface<PythonFfiDelegate<Object?>, Object?>
Gets a method from the class.
inherited
hasAttribute(String attributeName) bool
Checks if the python object has the given attribute.
inherited
noSuchMethod(Invocation invocation) Object?
Invoked when a nonexistent method or property is accessed.
inherited
setAttribute<T extends Object?>(String attributeName, T value) → void
Sets the attribute with the given name.
inherited
setAttributeRaw<T extends PythonObjectInterface<PythonFfiDelegate<Object?>, Object?>>(String attributeName, T value) → void
Sets the attribute with the given name.
inherited
toDartObject() Object?
Converts the python object to a Dart object.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited