Repository: pypa/setuptools

Issue:
Intention didn't match implementation
# Problem

Intention of the following block of code is to provide default value in case if it is missing in the config, but instead it raises `AttributeError` if `dist` has no `include_package_data` attribute.

https://github.com/pypa/setuptools/blob/92ebeed732b08ac29576634ad4814b9efd07bb37/setuptools/config/pyprojecttoml.py#L115-L118

# Suggested solution

Add `None` default value to make `getattr` happy.

# Origins

This issue was originally reported at Gentoo IRC channel.




<details>
<summary>System settings</summary>

```
# emerge --info '=dev-python/autocommand-2.2.2::gentoo'
Portage 3.0.39 (python 3.10.8-final-0, default/linux/amd64/17.1, gcc-12, glibc-2.36-r5, 6.0.9-gentoo-x86_64 x86_64)
=================================================================
                         System Settings
=================================================================
System uname: Linux-6.0.9-gentoo-x86_64-x86_64-12th_Gen_Intel-R-_Core-TM-_i7-1255U-with-glibc2.36
KiB Mem:    16060940 total,  13541120 free
KiB Swap:   33554428 total,  33554428 free
Timestamp of repository gentoo: Fri, 25 Nov 2022 17:30:01 +0000
Head commit of repository gentoo: 71d2c3534a720e321ec04e3beeba0e92e3e51e31
Timestamp of repository guru: Wed, 23 Nov 2022 01:38:02 +0000
Head commit of repository guru: 86a2bac9e60c7f1958df39e597d8e81389b0bca5

Timestamp of repository haskell: Thu, 24 Nov 2022 20:54:02 +0000
Head commit of repository haskell: 9f07eb3c68d93aa25a4d84e24b25b16a0ef8cf57

Timestamp of repository qt: Mon, 21 Nov 2022 13:16:47 +0000
Head commit of repository qt: 0bffd920e32aa057e2fc07f95c1b7e60329b7d4b

sh bash 5.2_p12
ld GNU ld (Gentoo 2.39 p5) 2.39.0
app-misc/pax-utils:        1.3.5::gentoo
app-shells/bash:           5.2_p12::gentoo
dev-java/java-config:      2.3.1::gentoo
dev-lang/perl:             5.36.0-r1::gentoo
dev-lang/python:           3.9.15_p3::gentoo, 3.10.8_p3::gentoo, 3.11.0_p2::gentoo
dev-lang/rust:             1.65.0::gentoo
dev-util/cmake:            3.25.0::gentoo
dev-util/meson:            0.64.1::gentoo
sys-apps/baselayout:       2.9::gentoo
sys-apps/openrc:           0.45.2-r1::gentoo
sys-apps/sandbox:          2.29::gentoo
sys-devel/autoconf:        2.71-r5::gentoo
sys-devel/automake:        1.16.5::gentoo
sys-devel/binutils:        2.39-r4::gentoo
sys-devel/binutils-config: 5.4.1::gentoo
sys-devel/clang:           15.0.5::gentoo
sys-devel/gcc:             12.2.1_p20221008::gentoo
sys-devel/gcc-config:      2.8::gentoo
sys-devel/libtool:         2.4.7::gentoo
sys-devel/lld:             15.0.5::gentoo
sys-devel/llvm:            15.0.5::gentoo
sys-devel/make:            4.4::gentoo
sys-kernel/linux-headers:  6.0::gentoo (virtual/os-headers)
sys-libs/glibc:            2.36-r5::gentoo
Repositories:

gentoo
    location: /var/db/repos/gentoo
    sync-type: rsync
    sync-uri: rsync://rsync.gentoo.org/gentoo-portage
    priority: -1000
    sync-rsync-verify-metamanifest: yes
    sync-rsync-verify-jobs: 1
    sync-rsync-extra-opts: 
    sync-rsync-verify-max-age: 24

guru
    location: /var/db/repos/guru
    sync-type: git
    sync-uri: https://github.com/gentoo-mirror/guru.git
    masters: gentoo

haskell
    location: /var/db/repos/haskell
    sync-type: git
    sync-uri: https://github.com/gentoo-mirror/haskell.git
    masters: gentoo

qt
    location: /var/db/repos/qt
    sync-type: git
    sync-uri: https://github.com/gentoo-mirror/qt.git
    masters: gentoo

ryanl-Gentoo
    location: /var/db/repos/ryanl-Gentoo
    masters: gentoo

ACCEPT_KEYWORDS="amd64 ~amd64"
ACCEPT_LICENSE="*"
CBUILD="x86_64-pc-linux-gnu"
CFLAGS="-march=alderlake -O2 -pipe"
CHOST="x86_64-pc-linux-gnu"
CONFIG_PROTECT="/etc /usr/lib64/libreoffice/program/sofficerc /usr/share/gnupg/qualified.txt"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/dconf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo"
CXXFLAGS="-march=alderlake -O2 -pipe"
DISTDIR="/var/cache/distfiles"
EMERGE_DEFAULT_OPTS="--jobs 3"
ENV_UNSET="CARGO_HOME DBUS_SESSION_BUS_ADDRESS DISPLAY GOBIN GOPATH PERL5LIB PERL5OPT PERLPREFIX PERL_CORE PERL_MB_OPT PERL_MM_OPT XAUTHORITY XDG_CACHE_HOME XDG_CONFIG_HOME XDG_DATA_HOME XDG_RUNTIME_DIR XDG_STATE_HOME"
FCFLAGS="-march=alderlake -O2 -pipe"
FEATURES="assume-digests binpkg-docompress binpkg-dostrip binpkg-logs binpkg-multi-instance buildpkg-live config-protect-if-modified distlocks ebuild-locks fixlafiles ipc-sandbox merge-sync multilib-strict network-sandbox news parallel-fetch pid-sandbox preserve-libs protect-owned qa-unresolved-soname-deps sandbox sfperms strict unknown-features-warn unmerge-logs unmerge-orphans userfetch userpriv usersandbox usersync xattr"
FFLAGS="-march=alderlake -O2 -pipe"
GENTOO_MIRRORS="http://mirror.rackspace.com/gentoo/     http://mirrors.rit.edu/gentoo/     https://mirror.rackspace.com/gentoo/"
LANG="C.UTF8"
LDFLAGS="-Wl,-O1 -Wl,--as-needed"
MAKEOPTS="-j7"
PKGDIR="/var/cache/binpkgs"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --omit-dir-times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages --exclude=/.git"
PORTAGE_TMPDIR="/var/tmp"
SHELL="/bin/bash"
USE="X acl alsa amd64 bluetooth bzip2 cli crypt dbus dri elogind emacs examples ffmpeg fortran gdbm graphviz gstreamer iconv ipv6 jumbo-build libglvnd libtirpc man multilib ncurses networkmanager nls nptl openmp pam pcre pipewire pipewire-alsa qt readline screencast seccomp sound split-usr ssl test-rust udev unicode usb wayland wayland-compositor wifi xattr xwayland zlib zstd" ABI_X86="64" ADA_TARGET="gnat_2021" APACHE2_MODULES="authn_core authz_core socache_shmcb unixd actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="karbon sheets words" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" CPU_FLAGS_X86="mmx mmxext sse sse2" ELIBC="glibc" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock greis isync itrax mtk3301 nmea ntrip navcom oceanserver oldstyle oncore rtcm104v2 rtcm104v3 sirf skytraq superstar2 timing tsip tripmate tnt ublox ubx" GRUB_PLATFORMS="efi-64" INPUT_DEVICES="libinput" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LIBREOFFICE_EXTENSIONS="presenter-console presenter-minimizer" LUA_SINGLE_TARGET="lua5-1" LUA_TARGETS="lua5-1" OFFICE_IMPLEMENTATION="libreoffice" PHP_TARGETS="php7-4 php8-0" POSTGRES_TARGETS="postgres12 postgres13" PYTHON_SINGLE_TARGET="python3_10" PYTHON_TARGETS="python3_10" RUBY_TARGETS="ruby27" USERLAND="GNU" VIDEO_CARDS="amdgpu fbdev intel nouveau radeon radeonsi vesa dummy v4l" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq proto steal rawnat logmark ipmark dhcpmac delude chaos account"
Unset:  ADDR2LINE, AR, ARFLAGS, AS, ASFLAGS, CC, CCLD, CONFIG_SHELL, CPP, CPPFLAGS, CTARGET, CXX, CXXFILT, ELFEDIT, EXTRA_ECONF, F77FLAGS, FC, GCOV, GPROF, INSTALL_MASK, LC_ALL, LD, LEX, LFLAGS, LIBTOOL, LINGUAS, MAKE, MAKEFLAGS, NM, OBJCOPY, OBJDUMP, PORTAGE_BINHOST, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS, RANLIB, READELF, RUSTFLAGS, SIZE, STRINGS, STRIP, YACC, YFLAGS
```
</details>

<details>
<summary>Build error</summary>

```
# emerge dev-python/autocommand

 * IMPORTANT: 2 config files in '/etc/portage' need updating.
Calculating dependencies  
 * IMPORTANT: 13 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.

 * See the CONFIGURATION FILES and CONFIGURATION FILES UPDATE TOOLS
 * sections of the emerge man page to learn how to update config files.
.... done!

!!! The following installed packages are masked:
- dev-qt/qtbase-6.4.0::qt (masked by: package.mask)
/var/db/repos/gentoo/profiles/package.mask:
# Jimi Huotari <chiitoo@gentoo.org> (2022-08-02)
# Masked for testing. The split of some packages may still
# change. bug #838970.

For more information, see the MASKED PACKAGES section in the emerge
man page or refer to the Gentoo Handbook.

>>> Verifying ebuild manifests
>>> Jobs: 0 of 1 complete, 1 running                Load avg: 2.02, 2.15, 1.59
>>> Emerging (1 of 1) dev-python/autocommand-2.2.2::gentoo
>>> Jobs: 0 of 1 complete, 1 running                Load avg: 2.02, 2.15, 1.59
>>> Failed to emerge dev-python/autocommand-2.2.2, Log file:
>>> Jobs: 0 of 1 complete, 1 running                Load avg: 2.02, 2.15, 1.59
>>>  '/var/tmp/portage/dev-python/autocommand-2.2.2/temp/build.log'
>>> Jobs: 0 of 1 complete, 1 running                Load avg: 2.02, 2.15, 1.59
>>> Jobs: 0 of 1 complete, 1 running, 1 failed      Load avg: 2.02, 2.15, 1.59
>>> Jobs: 0 of 1 complete, 1 failed                 Load avg: 2.02, 2.15, 1.59

 * Package:    dev-python/autocommand-2.2.2:0
 * Repository: gentoo
 * Maintainer: python@gentoo.org
 * USE:        abi_x86_64 amd64 elibc_glibc kernel_linux python_targets_python3_10 python_targets_python3_9 userland_GNU
 * FEATURES:   network-sandbox preserve-libs sandbox userpriv usersandbox
>>> Unpacking source...
>>> Unpacking autocommand-2.2.2.gh.tar.gz to /var/tmp/portage/dev-python/autocommand-2.2.2/work
>>> Source unpacked in /var/tmp/portage/dev-python/autocommand-2.2.2/work
>>> Preparing source in /var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2 ...
 * Build system packages:
 *   dev-python/gpep517            : 11
 *   dev-python/installer          : 0.5.1-r1
 *   dev-python/setuptools         : 65.6.3
 *   dev-python/setuptools_scm     : 7.0.5
 *   dev-python/wheel              : 0.38.4
>>> Source prepared.
>>> Configuring source in /var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2 ...
>>> Source configured.
>>> Compiling source in /var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2 ...
 * python3_9: running distutils-r1_run_phase distutils-r1_python_compile
 *   Building the wheel for autocommand-2.2.2 via setuptools.build_meta:__legacy__
gpep517 build-wheel --backend setuptools.build_meta:__legacy__ --output-fd 3 --wheel-dir /var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2-python3_9/wheel
2022-11-26 13:51:59,774 gpep517 INFO Building wheel via backend setuptools.build_meta:__legacy__
Traceback (most recent call last):
  File "/usr/lib/python-exec/python3.9/gpep517", line 4, in <module>
    sys.exit(main())
  File "/usr/lib/python3.9/site-packages/gpep517/__main__.py", line 339, in main
    return func(args)
  File "/usr/lib/python3.9/site-packages/gpep517/__main__.py", line 139, in build_wheel
    print(build_wheel_impl(args, args.wheel_dir), file=out)
  File "/usr/lib/python3.9/site-packages/gpep517/__main__.py", line 131, in build_wheel_impl
    wheel_name = backend.build_wheel(str(wheel_dir), args.config_json)
  File "/usr/lib/python3.9/site-packages/setuptools/build_meta.py", line 413, in build_wheel
    return self._build_with_temp_dir(['bdist_wheel'], '.whl',
  File "/usr/lib/python3.9/site-packages/setuptools/build_meta.py", line 398, in _build_with_temp_dir
    self.run_setup()
  File "/usr/lib/python3.9/site-packages/setuptools/build_meta.py", line 484, in run_setup
    super(_BuildMetaLegacyBackend,
  File "/usr/lib/python3.9/site-packages/setuptools/build_meta.py", line 335, in run_setup
    exec(code, locals())
  File "<string>", line 8, in <module>
  File "/usr/lib/python3.9/site-packages/setuptools/__init__.py", line 87, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.9/site-packages/setuptools/_distutils/core.py", line 159, in setup
    dist.parse_config_files()
  File "/usr/lib/python3.9/site-packages/setuptools/dist.py", line 867, in parse_config_files
    pyprojecttoml.apply_configuration(self, filename, ignore_option_errors)
  File "/usr/lib/python3.9/site-packages/setuptools/config/pyprojecttoml.py", line 62, in apply_configuration
    config = read_configuration(filepath, True, ignore_option_errors, dist)
  File "/usr/lib/python3.9/site-packages/setuptools/config/pyprojecttoml.py", line 115, in read_configuration
    if dist and getattr(dist, "include_package_data") is not None:
AttributeError: 'Distribution' object has no attribute 'include_package_data'
 * ERROR: dev-python/autocommand-2.2.2::gentoo failed (compile phase):
 *   Wheel build failed
 * 
 * Call stack:
 *     ebuild.sh, line  122:  Called src_compile
 *   environment, line 3304:  Called distutils-r1_src_compile
 *   environment, line 1534:  Called _distutils-r1_run_foreach_impl 'distutils-r1_python_compile'
 *   environment, line  687:  Called python_foreach_impl 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 2997:  Called multibuild_foreach_variant '_python_multibuild_wrapper' 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 2560:  Called _multibuild_run '_python_multibuild_wrapper' 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 2558:  Called _python_multibuild_wrapper 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 1010:  Called distutils-r1_run_phase 'distutils-r1_python_compile'
 *   environment, line 1516:  Called distutils-r1_python_compile
 *   environment, line 1340:  Called distutils_pep517_install '/var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2-python3_9/install'
 *   environment, line 1834:  Called die
 * The specific snippet of code:
 *       local wheel=$("${cmd[@]}" 3>&1 1>&2 || die "Wheel build failed");
 * 
 * If you need support, post the output of `emerge --info '=dev-python/autocommand-2.2.2::gentoo'`,
 * the complete build log and the output of `emerge -pqv '=dev-python/autocommand-2.2.2::gentoo'`.
 * The complete build log is located at '/var/tmp/portage/dev-python/autocommand-2.2.2/temp/build.log'.
 * The ebuild environment file is located at '/var/tmp/portage/dev-python/autocommand-2.2.2/temp/environment'.
 * Working directory: '/var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2'
 * S: '/var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2'
 * Messages for package dev-python/autocommand-2.2.2:
 * ERROR: dev-python/autocommand-2.2.2::gentoo failed (compile phase):
 *   Wheel build failed
 * 
 * Call stack:
 *     ebuild.sh, line  122:  Called src_compile
 *   environment, line 3304:  Called distutils-r1_src_compile
 *   environment, line 1534:  Called _distutils-r1_run_foreach_impl 'distutils-r1_python_compile'
 *   environment, line  687:  Called python_foreach_impl 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 2997:  Called multibuild_foreach_variant '_python_multibuild_wrapper' 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 2560:  Called _multibuild_run '_python_multibuild_wrapper' 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 2558:  Called _python_multibuild_wrapper 'distutils-r1_run_phase' 'distutils-r1_python_compile'
 *   environment, line 1010:  Called distutils-r1_run_phase 'distutils-r1_python_compile'
 *   environment, line 1516:  Called distutils-r1_python_compile
 *   environment, line 1340:  Called distutils_pep517_install '/var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2-python3_9/install'
 *   environment, line 1834:  Called die
 * The specific snippet of code:
 *       local wheel=$("${cmd[@]}" 3>&1 1>&2 || die "Wheel build failed");
 * 
 * If you need support, post the output of `emerge --info '=dev-python/autocommand-2.2.2::gentoo'`,
 * the complete build log and the output of `emerge -pqv '=dev-python/autocommand-2.2.2::gentoo'`.
 * The complete build log is located at '/var/tmp/portage/dev-python/autocommand-2.2.2/temp/build.log'.
 * The ebuild environment file is located at '/var/tmp/portage/dev-python/autocommand-2.2.2/temp/environment'.
 * Working directory: '/var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2'
 * S: '/var/tmp/portage/dev-python/autocommand-2.2.2/work/autocommand-2.2.2'
```
</details>


Discussion:

Resolving patch:
diff --git a/setuptools/config/pyprojecttoml.py b/setuptools/config/pyprojecttoml.py
--- a/setuptools/config/pyprojecttoml.py
+++ b/setuptools/config/pyprojecttoml.py
@@ -112,7 +112,7 @@ def read_configuration(
     # `ini2toml` backfills include_package_data=False when nothing is explicitly given,
     # therefore setting a default here is backwards compatible.
     orig_setuptools_table = setuptools_table.copy()
-    if dist and getattr(dist, "include_package_data") is not None:
+    if dist and getattr(dist, "include_package_data", None) is not None:
         setuptools_table.setdefault("include-package-data", dist.include_package_data)
     else:
         setuptools_table.setdefault("include-package-data", True)


Test patch: