//python/private:py_info.bzl

Implementation of PyInfo provider and PyInfo-specific utilities.

providerPyInfo

Encapsulates information provided by the Python rules.

Instead of creating this object directly, usePyInfoBuilder andthePyCommonApi utilities.

PyInfo.<init>(direct_original_sources,direct_pyc_files,direct_pyi_files,has_py2_only_sources,has_py3_only_sources,imports,transitive_implicit_pyc_files,transitive_implicit_pyc_source_files,transitive_original_sources,transitive_pyc_files,transitive_pyi_files,transitive_sources,uses_shared_libraries,venv_symlinks)
PyInfo.direct_original_sources:depset[File]

The.py source files (if any) that are considered directly provided bythe target. This field is intended so that static analysis tools can recover theoriginal Python source files, regardless of any build settings (e.g.precompiling), so they can analyze source code. The values are typically the.py files in thesrcs attribute (or equivalent).

Added in version 1.1.0.

PyInfo.direct_pyc_files:depset[File]

Precompiled Python files that are considered directly providedby the target andmust be included.

These files usually come from, e.g., a library settingprecompile=enabledto forcibly enable precompiling for itself. Downstream binaries are expectedto always include these files, as the originating target expects them to exist.

PyInfo.direct_pyi_files:depset[File]

Type definition files (usually.pyi files) for the Python modules provided bythis target. Usually they describe the source files listed indirect_original_sources. This field is primarily for static analysis tools.

These files areusually build-time only and not included as part of a runnableprogram.

Note

This may contain implementation-specific file types specific to a particulartype checker.

Added in version 1.1.0.

PyInfo.has_py2_only_sources:bool

Whether any of this target’s transitive sources requires a Python 2 runtime.

PyInfo.has_py3_only_sources:bool

Whether any of this target’s transitive sources requires a Python 3 runtime.

PyInfo.imports:depset[str]

A depset of import path strings to be added to thePYTHONPATH of executablePython targets. These are accumulated from the transitivedeps.The order of the depset is not guaranteed and may be changed in the future. Itis recommended to usedefault order (the default).

PyInfo.transitive_implicit_pyc_files:depset[File]

Automatically generated pyc files that downstream binaries (or equivalent)can choose to include in their output. If not included, thentransitive_implicit_pyc_source_files should be included instead.

Added in version 0.37.0.

PyInfo.transitive_implicit_pyc_source_files:depset[File]

Source.py files fortransitive_implicit_pyc_files that downstreambinaries (or equivalent) can choose to include in their output. If not included,thentransitive_implicit_pyc_files should be included instead.

Added in version 0.37.0.

PyInfo.transitive_original_sources:depset[File]

The transitive set of.py source files (if any) that are considered theoriginal sources for this target and its transitive dependencies. This field isintended so that static analysis tools can recover the original Python sourcefiles, regardless of any build settings (e.g. precompiling), so they can analyzesource code. The values are typically the.py files in thesrcs attribute(or equivalent).

This is superset ofdirect_original_sources.

Added in version 1.1.0.

PyInfo.transitive_pyc_files:depset[File]

The transitive set of precompiled files that must be included.

These files usually come from, e.g., a library settingprecompile=enabledto forcibly enable precompiling for itself. Downstream binaries are expectedto always include these files, as the originating target expects them to exist.

PyInfo.transitive_pyi_files:depset[File]

The transitive set of type definition files (usually.pyi files) for thePython modules for this target and its transitive dependencies. this target.Usually they describe the source files listed intransitive_original_sources.This field is primarily for static analysis tools.

These files areusually build-time only and not included as part of a runnableprogram.

Note

This may contain implementation-specific file types specific to a particulartype checker.

Added in version 1.1.0.

PyInfo.transitive_sources:depset[File]

A (postorder-compatible) depset of.py files that are considered requiredand downstream binaries (or equivalent)must include in their outputsto have a functioning program.

Normally, these are the.py files in the appearing in the target’ssrcs andthesrcs of the target’s transitivedeps,however, precompile settingsmay cause.py files to be omitted. In particular, pyc-only builds may resultin this depset beingempty.

Changed in version 0.37.0:The files are considered necessary for downstream binaries to function;previously they were considerd informational and largely unused.

PyInfo.uses_shared_libraries:bool

Whether any of this target’s transitivedeps has a shared library file (suchas a.so file).

This field is currently unused in Bazel and may go away in the future.

PyInfo.venv_symlinks:depset[VenvSymlinkEntry]

Warning

Experimental API. This API is still under development and may change or beremoved without notice.

Added in version 1.5.0.

typedefPyInfoBuilder

Builder for PyInfo.

To create an instance, usepy_common.get() and callPyInfoBuilder()

PyInfoBuilder.direct_original_sources:DepsetBuilder[File]
PyInfoBuilder.direct_pyc_files:DepsetBuilder[File]
PyInfoBuilder.direct_pyi_files:DepsetBuilder[File]
PyInfoBuilder.imports:DepsetBuilder[str]
PyInfoBuilder.transitive_implicit_pyc_files:DepsetBuilder[File]
PyInfoBuilder.transitive_implicit_pyc_source_files:DepsetBuilder[File]
PyInfoBuilder.transitive_original_sources:DepsetBuilder[File]
PyInfoBuilder.transitive_pyc_files:DepsetBuilder[File]
PyInfoBuilder.transitive_pyi_files:DepsetBuilder[File]
PyInfoBuilder.transitive_sources:DepsetBuilder[File]
PyInfoBuilder.venv_symlinks:DepsetBuilder[tuple[str|None,str]]
venv_symlinks.build()

Builds into aPyInfo object.

Returns:

PyInfo

PyInfoBuilder.build_builtin_py_info()

Builds into a Bazel-builtin PyInfo object, if available.

Returns:

BuiltinPyInfo|None None is returned if Bazel’sbuiltin PyInfo object is disabled.

PyInfoBuilder.get_has_py2_only_sources()

Get thehas_py2_only_sources value.

Returns:

bool

PyInfoBuilder.get_has_py3_only_sources()

Get thehas_py3_only_sources value.

Returns:

bool

PyInfoBuilder.get_uses_shared_libraries()

Get theuses_shared_libraries value.

Returns:

bool

PyInfoBuilder.merge(direct=[],*infos)

Merge other PyInfos into this PyInfo.

Args:
  • direct(list[PyInfo])(default[])

    objects to merge in, but also merge theirdirect fields into this object’s direct fields.

  • infos(PyInfo)

    objects to merge in, but only merge in theirinformation into this object’s transitive fields.

Returns:

PyInfoBuilder self

PyInfoBuilder.merge_all(transitive,direct=[])

Merge other PyInfos into this PyInfo.

Args:
  • transitive(list[PyInfo])

    objects to merge in, but only merge intheir information into this object’s transitive fields.

  • direct(list[PyInfo])(default[])

    objects to merge in, but also merge theirdirect fields into this object’s direct fields.

Returns:

PyInfoBuilder self

PyInfoBuilder.merge_has_py2_only_sources(value)

Setshas_py2_only_sources based on current and incomingvalue.

Args:
  • value(bool)

    Anotherhas_py2_only_sources value. It willbe merged into this builder’s state.

Returns:

PyInfoBuilder self

PyInfoBuilder.merge_has_py3_only_sources(value)

Setshas_py3_only_sources based on current and incomingvalue.

Args:
  • value(bool)

    Anotherhas_py3_only_sources value. It willbe merged into this builder’s state.

Returns:

PyInfoBuilder self

PyInfoBuilder.merge_target(target)

Merge a target’s Python information in this object.

Args:
  • target(Target)

    targets that provide PyInfo, or other relevantproviders, will be merged into this object. If a target doesn’t provideany relevant providers, it is ignored.

Returns:

PyInfoBuilder self.

PyInfoBuilder.merge_targets(targets)

Merge multiple targets into this object.

Args:
  • targets(list[Target])

    targets that provide PyInfo, or other relevantproviders, will be merged into this object. If a target doesn’t provideany relevant providers, it is ignored.

Returns:

PyInfoBuilder self.

PyInfoBuilder.merge_uses_shared_libraries(value)

Setsuses_shared_libraries based on current and incomingvalue.

Args:
  • value(bool)

    Anotheruses_shared_libraries value. It willbe merged into this builder’s state.

Returns:

PyInfoBuilder self

PyInfoBuilder.new()

Creates an instance.

Returns:

PyInfoBuilder

PyInfoBuilder.set_has_py2_only_sources(value)

Setshas_py2_only_sources tovalue.

Args:
Returns:

PyInfoBuilder self

PyInfoBuilder.set_has_py3_only_sources(value)

Setshas_py3_only_sources tovalue.

Args:
Returns:

PyInfoBuilder self

PyInfoBuilder.set_uses_shared_libraries(value)

Setsuses_shared_libraries tovalue.

Args:
Returns:

PyInfoBuilder self

providerVenvSymlinkEntry

An entry inPyInfo.venv_symlinks

VenvSymlinkEntry.<init>(files,kind,link_to_file,link_to_path,package,venv_path,version)
VenvSymlinkEntry.files:depset[File]

Files underlink_to_path.

This is only used when multiple targets have overlappingvenv_path paths. e.g.if one adds files tovenv_path=a/ and another adds files tovenv_path=a/b/.

VenvSymlinkEntry.kind:str

One of theVenvSymlinkKind values. It represents which directory withinthe venv to create the path under.

VenvSymlinkEntry.link_to_file:File|None

A file thatvenv_path should point to. The file to link to should also be infiles.

Added in version 1.7.0.

VenvSymlinkEntry.link_to_path:str|None

A runfiles-root relative path thatvenv_path will symlink to (iflink_to_file isNone). IfNone, it means to not create it in the venv.

VenvSymlinkEntry.package:str|None

Represents the PyPI package name that the code originates from. It is normalized according to thePEP440 with all- replaced with_, i.e. the same as the package name in the hub repository thatit would come from.

VenvSymlinkEntry.venv_path:str

A path relative to thekind directory within the venv.

VenvSymlinkEntry.version:str|None

Represents the PyPI package version that the code originates from. It is normalized according to thePEP440 standard.

typedefVenvSymlinkKind

An enum of types of venv directories.

VenvSymlinkKind.BIN:object

Indicates to create paths under the directory that has binarieswithin the venv.

VenvSymlinkKind.LIB:object

Indicates to create paths under the venv’s site-packages directory.

VenvSymlinkKind.INCLUDE:object

Indicates to create paths under the venv’s include directory.