Repository: conan-io/conan
Pull request: Don't break on empty patch list for version in conandata.yml (#15841)
Changelog: Bugfix: Avoid TypeError when a version in conandata.yml lists no patches.
Docs: Omit

 (#15841)

- [x] Refer to the issue that supports this Pull Request.
- [ ] If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
- [x] I've read the [Contributing guide](https://github.com/conan-io/conan/blob/develop/.github/CONTRIBUTING.md).
- [x] I've followed the PEP8 style guides for Python code.
- [ ] I've opened another PR in the Conan docs repo to the ``develop`` branch, documenting this one.


Problem statement:
[bug] Breakage when all patches in conandata.yml are commented out
### Environment details

* Operating System+version: RHEL 8
* Compiler+version: N/A
* Conan version: Git
* Python version: 3.11.5


### Steps to reproduce

I was testing out various permutations of a Conan package build. At one point, I commented out all the patches listed for the version of interest in `conandata.yml`, so it looked like
```
patches:
  "1.2.3":
    #- patch_file: patches/0001-first.patch
    #- patch_file: patches/0002-second.patch
    #- patch_file: patches/0003-third.patch
    #- patch_file: patches/0004-fourth.patch
```

When I started the `conan create`, however, I  got this:
```
======== Exporting recipe to the cache ========
package/1.2.3: Exporting package recipe: /home/src/package/conanfile.py
package/1.2.3: exports: File 'conandata.yml' found. Exporting it...
package/1.2.3: Calling export_sources()
ERROR: 
	TypeError: 'NoneType' object is not iterable
```

It's a simple fix; I will submit it shortly.

### Logs

_No response_

Hints from issue discussion:

Commit message: Don't break on empty patch list for version in conandata.yml (#15841)

Patch to review:
diff --git a/conan/tools/files/patches.py b/conan/tools/files/patches.py
index d3e48d62f53..2add350bac5 100644
--- a/conan/tools/files/patches.py
+++ b/conan/tools/files/patches.py
@@ -88,6 +88,9 @@ def apply_conandata_patches(conanfile):
     if isinstance(patches, dict):
         assert conanfile.version, "Can only be applied if conanfile.version is already defined"
         entries = patches.get(str(conanfile.version), [])
+        if entries is None:
+            conanfile.output.info("apply_conandata_patches(): No patches defined for version in conandata")
+            return
     elif isinstance(patches, list):
         entries = patches
     else:
@@ -127,6 +130,9 @@ def export_conandata_patches(conanfile):
     if isinstance(patches, dict):
         assert conanfile.version, "Can only be exported if conanfile.version is already defined"
         entries = patches.get(conanfile.version, [])
+        if entries is None:
+            conanfile.output.info("export_conandata_patches(): No patches defined for version in conandata")
+            return
     elif isinstance(patches, list):
         entries = patches
     else:


Code review comments:
diff_hunk: @@ -127,6 +130,9 @@ def export_conandata_patches(conanfile):
     if isinstance(patches, dict):
         assert conanfile.version, "Can only be exported if conanfile.version is already defined"
         entries = patches.get(conanfile.version, [])
+        if entries is None:
+            conanfile.output.info("export_conandata_patches(): No patches defined for version in conandata")
path: conan/tools/files/patches.py
text: @user1:
```suggestion
            conanfile.output.warning(f"export_conandata_patches(): No patches defined for version {conanfile.version} in conandata.yml")
```