{"record":{"id":"a68ca9a42b3edc94","repo":"pypa/pip","slug":"expected-str-requirement-or-distribution","errorCode":null,"errorMessage":"Expected str, Requirement, or Distribution","messagePattern":"Expected str, Requirement, or Distribution","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pkg_resources/__init__.py","lineNumber":543,"sourceCode":"        return True\n\n    # XXX Linux and other platforms' special cases should go here\n    return False\n\n\n@overload\ndef get_distribution(dist: _DistributionT) -> _DistributionT: ...\n@overload\ndef get_distribution(dist: _PkgReqType) -> Distribution: ...\ndef get_distribution(dist: Distribution | _PkgReqType) -> Distribution:\n    \"\"\"Return a current distribution object for a Requirement or string\"\"\"\n    if isinstance(dist, str):\n        dist = Requirement.parse(dist)\n    if isinstance(dist, Requirement):\n        # Bad type narrowing, dist has to be a Requirement here, so get_provider has to return Distribution\n        dist = get_provider(dist)  # type: ignore[assignment]\n    if not isinstance(dist, Distribution):\n        raise TypeError(\"Expected str, Requirement, or Distribution\", dist)\n    return dist\n\n\ndef load_entry_point(dist: _EPDistType, group: str, name: str) -> _ResolvedEntryPoint:\n    \"\"\"Return `name` entry point of `group` for `dist` or raise ImportError\"\"\"\n    return get_distribution(dist).load_entry_point(group, name)\n\n\n@overload\ndef get_entry_map(\n    dist: _EPDistType, group: None = None\n) -> dict[str, dict[str, EntryPoint]]: ...\n@overload\ndef get_entry_map(dist: _EPDistType, group: str) -> dict[str, EntryPoint]: ...\ndef get_entry_map(dist: _EPDistType, group: str | None = None):\n    \"\"\"Return the entry point map for `group`, or the full entry map\"\"\"\n    return get_distribution(dist).get_entry_map(group)\n","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/pkg_resources/__init__.py#L525-L561","documentation":"Raised by pkg_resources.get_distribution() when the argument is not a str, a Requirement, nor a Distribution. The function accepts a project name string, a requirement string, a Requirement object, or an existing Distribution; after normalization, if the result is none of these, TypeError is raised. This is a type-contract violation by the caller.","triggerScenarios":"Calling get_distribution(None), get_distribution(123), get_distribution(['flask']), or any value whose type is not str/Requirement/Distribution.","commonSituations":"A variable that was expected to hold a package name string but is None (e.g. a missing config key); passing a list or dict where a scalar name was expected; a refactoring that changed the type flowing into get_distribution.","solutions":["Check that the argument is a str before calling: assert isinstance(name, str).","Fix the upstream code to always supply a non-None package name string.","Use a type guard or explicit conversion to str before calling get_distribution()."],"exampleFix":"// before\ndist = get_distribution(maybe_none)\n// after\nif maybe_none is None:\n    raise ValueError('package name is required')\ndist = get_distribution(maybe_none)","handlingStrategy":"type-guard","validationCode":"if not isinstance(dist, (str, Requirement, Distribution)):\n    raise TypeError(f'Expected str/Requirement/Distribution, got {type(dist)}')\nget_distribution(dist)","typeGuard":"import pkg_resources\ndef is_valid_dist_arg(d) -> bool:\n    return isinstance(d, (str, pkg_resources.Requirement, pkg_resources.Distribution))","tryCatchPattern":"try:\n    d = get_distribution(arg)\nexcept TypeError:\n    # caller passed a wrong type\n    d = None","preventionTips":["Guard None at the source rather than relying on get_distribution to reject it.","Use type hints to catch mismatches before runtime."],"tags":["pkg-resources","type-error","validation"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}