@@ -356,6 +356,45 @@ def _setuptools_upper_bound(self):
356356 return "82"
357357 return "83"
358358
359+ def _detect_setup_py_for_package_config (self ):
360+ """Dynamically find out if setup.py is used for package configuration.
361+
362+ If `pyproject.toml` contains some specific content, we assume that this
363+ file is used for package configuration, and `setup.py` is basically empty.
364+ Main reason: in that case `check-python-versions` fails if we ask it to
365+ check `setup.py`.
366+ """
367+ if not (self .path / "setup.py" ).exists ():
368+ # If the file does not even exist, it is definitely not used. ;-)
369+ return False
370+ pyproject = self .path / "pyproject.toml"
371+ if not pyproject .exists ():
372+ # Should not happen. But if this file does not exist, then
373+ # `setup.py` will be used.
374+ return True
375+ text = pyproject .read_text ()
376+ # If any of these markers do NOT exist in the `pyproject.toml` file,
377+ # we conclude that `setup.py` is still used.
378+ markers = [
379+ "START-MARKER-MANUAL-CONFIG" ,
380+ "[project]" ,
381+ "Programming Language :: Python" ,
382+ "requires-python" ,
383+ ]
384+ return any ([marker not in text for marker in markers ])
385+
386+ def _check_python_versions_files (self , using_setup_py ):
387+ """Which files should be checked by check-python-versions?
388+
389+ This is for inclusion in the pre-commit config:
390+
391+ id: check-python-versions
392+ args: ['--only', 'setup.py,pyproject.toml']
393+ """
394+ if using_setup_py :
395+ return "setup.py,tox.ini"
396+ return "pyproject.toml,tox.ini"
397+
359398 def pre_commit_config (self ):
360399 options = self ._get_options_for (
361400 "pre_commit" ,
@@ -370,6 +409,9 @@ def pre_commit_config(self):
370409
371410 python_version = self ._minimal_python_version ()
372411 options ["minimal_python_version" ] = self ._no_dot_python_version (python_version )
412+ options ["check_python_versions_files" ] = self ._check_python_versions_files (
413+ self ._detect_setup_py_for_package_config ()
414+ )
373415
374416 return self .copy_with_meta (
375417 "pre-commit-config.yaml.j2" ,
0 commit comments