MCPcopy
hub / github.com/python-attrs/attrs / fields

Function fields

src/attr/_make.py:1891–1939  ·  view source on GitHub ↗

Return the tuple of *attrs* attributes for a class or instance. The tuple also allows accessing the fields by their names (see below for examples). Args: cls (type): Class or instance to introspect. Raises: TypeError: If *cls* is neither a class nor an *attrs*

(cls)

Source from the content-addressed store, hash-verified

1889
1890
1891def fields(cls):
1892 """
1893 Return the tuple of *attrs* attributes for a class or instance.
1894
1895 The tuple also allows accessing the fields by their names (see below for
1896 examples).
1897
1898 Args:
1899 cls (type): Class or instance to introspect.
1900
1901 Raises:
1902 TypeError: If *cls* is neither a class nor an *attrs* instance.
1903
1904 attrs.exceptions.NotAnAttrsClassError:
1905 If *cls* is not an *attrs* class.
1906
1907 Returns:
1908 tuple (with name accessors) of `attrs.Attribute`
1909
1910 .. versionchanged:: 16.2.0 Returned tuple allows accessing the fields
1911 by name.
1912 .. versionchanged:: 23.1.0 Add support for generic classes.
1913 .. versionchanged:: 26.1.0 Add support for instances.
1914 """
1915 generic_base = get_generic_base(cls)
1916
1917 if generic_base is None and not isinstance(cls, type):
1918 type_ = type(cls)
1919 if getattr(type_, "__attrs_attrs__", None) is None:
1920 msg = "Passed object must be a class or attrs instance."
1921 raise TypeError(msg)
1922
1923 return fields(type_)
1924
1925 attrs = getattr(cls, "__attrs_attrs__", None)
1926
1927 if attrs is None:
1928 if generic_base is not None:
1929 attrs = getattr(generic_base, "__attrs_attrs__", None)
1930 if attrs is not None:
1931 # Even though this is global state, stick it on here to speed
1932 # it up. We rely on `cls` being cached for this to be
1933 # efficient.
1934 cls.__attrs_attrs__ = attrs
1935 return attrs
1936 msg = f"{cls!r} is not an attrs-decorated class."
1937 raise NotAnAttrsClassError(msg)
1938
1939 return attrs
1940
1941
1942def fields_dict(cls):

Callers 15

test_typesMethod · 0.90
test_instanceMethod · 0.90
test_fieldsMethod · 0.90
test_genericsMethod · 0.90
test_fields_dictMethod · 0.90
test_metadata_presentMethod · 0.90

Calls 2

get_generic_baseFunction · 0.85

Tested by 15

test_typesMethod · 0.72
test_instanceMethod · 0.72
test_fieldsMethod · 0.72
test_genericsMethod · 0.72
test_fields_dictMethod · 0.72
test_metadata_presentMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…