Repository: conan-io/conan
Pull request #16224 (2024-05-08 14:58:21)

Fix/export pkg version pyrequire
Changelog: Bugfix: Allow ``export-pkg --version=xxx`` to be passed to recipes with ``python_requires`` inhering ``set_version`` from base class
Docs: Omit

Close https://github.com/conan-io/conan/issues/16223


Patch:
diff --git a/conan/api/subapi/local.py b/conan/api/subapi/local.py
index 88ae8a8fe..27ed46e00 100644
--- a/conan/api/subapi/local.py
+++ b/conan/api/subapi/local.py
@@ -111,9 +111,10 @@ class LocalAPI:
             with chdir(conanfile.build_folder):
                 conanfile.test()
 
-    def inspect(self, conanfile_path, remotes, lockfile):
+    def inspect(self, conanfile_path, remotes, lockfile, name=None, version=None, user=None,
+                channel=None):
         app = ConanApp(self._conan_api)
-        conanfile = app.loader.load_named(conanfile_path, name=None, version=None,
-                                          user=None, channel=None, remotes=remotes, graph_lock=lockfile)
+        conanfile = app.loader.load_named(conanfile_path, name=name, version=version, user=user,
+                                          channel=channel, remotes=remotes, graph_lock=lockfile)
         return conanfile
 
diff --git a/conan/cli/commands/export_pkg.py b/conan/cli/commands/export_pkg.py
index c9a6e9438..a47cdfe46 100644
--- a/conan/cli/commands/export_pkg.py
+++ b/conan/cli/commands/export_pkg.py
@@ -46,7 +46,8 @@ def export_pkg(conan_api, parser, *args):
     profile_host, profile_build = conan_api.profiles.get_profiles_from_args(args)
     remotes = conan_api.remotes.list(args.remote) if not args.no_remote else []
 
-    conanfile = conan_api.local.inspect(path, remotes, lockfile)
+    conanfile = conan_api.local.inspect(path, remotes, lockfile, name=args.name,
+                                        version=args.version, user=args.user, channel=args.channel)
     # The package_type is not fully processed at export
     if conanfile.package_type == "python-require":
         raise ConanException("export-pkg can only be used for binaries, not for 'python-require'")


Test patch:
diff --git a/conans/test/integration/command/export_pkg_test.py b/conans/test/integration/command/export_pkg_test.py
index af0d6be38..3c52b662e 100644
--- a/conans/test/integration/command/export_pkg_test.py
+++ b/conans/test/integration/command/export_pkg_test.py
@@ -638,3 +638,34 @@ def test_remote_none_tool_requires():
     c.run(f"export-pkg pkg {settings} -s:b compiler.cppstd=17")  # This used to crash
     # No longer crash
     assert "pkg/0.1 (test package): Running test()" in c.out
+
+
+def test_export_pkg_set_version_python_requires():
+    # https://github.com/conan-io/conan/issues/16223
+    c = TestClient(light=True)
+    py_require = textwrap.dedent("""
+        from conan import ConanFile
+
+        class TestBuild:
+            def set_version(self):
+                assert self.version
+
+        class TestModule(ConanFile):
+            name = "pyreq"
+            version = "0.1"
+            package_type = "python-require"
+    """)
+    pkg = textwrap.dedent("""
+        from conan import ConanFile
+
+        class TestPkgConan(ConanFile):
+            name="testpkg"
+            python_requires = "pyreq/0.1"
+            python_requires_extend = "pyreq.TestBuild"
+        """)
+    c.save({"pyreq/conanfile.py": py_require,
+            "pkg/conanfile.py": pkg})
+
+    c.run("create pyreq")
+    c.run("export-pkg pkg --version=1.0+0")
+    assert "conanfile.py (testpkg/1.0+0): Exported package binary" in c.out