Title: Can not call method in parent class, when using nested classes with inheritance · Issue #1420 · pythonnet/pythonnet · GitHub
Open Graph Title: Can not call method in parent class, when using nested classes with inheritance · Issue #1420 · pythonnet/pythonnet
X Title: Can not call method in parent class, when using nested classes with inheritance · Issue #1420 · pythonnet/pythonnet
Description: Environment Pythonnet version: 2.5.1 Python version: Python 3.9.2 Operating System: MacOS .NET Runtime: 5.0.200 Details Related to the problem reported in #1414 I.e. classes-in-classes are not handled properly in Pythonnet. Even when app...
Open Graph Description: Environment Pythonnet version: 2.5.1 Python version: Python 3.9.2 Operating System: MacOS .NET Runtime: 5.0.200 Details Related to the problem reported in #1414 I.e. classes-in-classes are not hand...
X Description: Environment Pythonnet version: 2.5.1 Python version: Python 3.9.2 Operating System: MacOS .NET Runtime: 5.0.200 Details Related to the problem reported in #1414 I.e. classes-in-classes are not hand...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/1420
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Can not call method in parent class, when using nested classes with inheritance","articleBody":"### Environment\r\n\r\nPythonnet version: 2.5.1\r\nPython version: Python 3.9.2\r\nOperating System: MacOS\r\n.NET Runtime: 5.0.200\r\n\r\n### Details\r\n\r\nRelated to the problem reported in #1414 \r\n\r\nI.e. classes-in-classes are not handled properly in Pythonnet.\r\n\r\nEven when applying the workaround mentioned in the #1414 issue there are still problems related to the inheritance.\r\n\r\nNamely, I can not call a method declared on the baseclass, on an instance of the nested child class.\r\n\r\nThe following code demonstrates the issue\r\n\r\n```cs\r\n\r\nnamespace pythonnet_testing {\r\n\r\n public class Bar {\r\n\r\n public string MethodInBaseClass() {\r\n return \"hello\";\r\n }\r\n\r\n public class Hej : Bar {\r\n }\r\n }\r\n\r\n public class Foo {\r\n public Bar Bar;\r\n\r\n public static Foo Create() {\r\n Foo f = new();\r\n f.Bar = new Bar.Hej();\r\n return f;\r\n }\r\n }\r\n\r\n class Program {\r\n static void Main(string[] args) {\r\n\r\n var pyCode = @\"\r\nimport pythonnet_testing\r\nf = pythonnet_testing.Foo.Create()\r\nprint('Trying to access member of nested subclass')\r\n_ = pythonnet_testing.Bar # NOTE: The python code crashes unless this explicit reference to the Bar class is used\r\nprint(f'Bar: {f.Bar}')\r\nprint(f'Method call: {f.Bar.MethodInBaseClass()}')\r\n\r\n \";\r\n\r\n PythonEngine.Initialize();\r\n\r\n using (Py.GIL()) {\r\n PythonEngine.Exec(pyCode);\r\n }\r\n }\r\n }\r\n```\r\n\r\nRunning this gives the following error message\r\n\r\n```\r\nTrying to access member of nested subclass\r\nBar: pythonnet_testing.Bar+Hej\r\nUnhandled exception. Python.Runtime.PythonException: AttributeError : 'Hej' object has no attribute 'MethodInBaseClass'\r\n[' File \"\u003cstring\u003e\", line 7, in \u003cmodule\u003e\\n'] at Python.Runtime.PythonException.ThrowIfIsNull(BorrowedReference reference)\r\n at Python.Runtime.PythonEngine.RunString(String code, Nullable`1 globals, Nullable`1 locals, RunFlagType flag)\r\n```\r\n\r\nIn order to call the `MethodInBaseClass` method on the child class, I must explicitly re-define it like this:\r\n\r\n```cs\r\n public class Hej : Bar {\r\n public new string MethodInBaseClass() =\u003e base.MethodInBaseClass();\r\n }\r\n```\r\n\r\n","author":{"url":"https://github.com/koliyo","@type":"Person","name":"koliyo"},"datePublished":"2021-03-18T15:28:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/1420/pythonnet/issues/1420"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2993ec71-18ba-dc18-8df9-5b6adc6d4876 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81BA:17D5BB:F0FFB8:14E4D69:6970C5A6 |
| html-safe-nonce | d72f0c1b28adffbad34684076485ecbb55456f794fe3754166e401dd500c31be |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUJBOjE3RDVCQjpGMEZGQjg6MTRFNEQ2OTo2OTcwQzVBNiIsInZpc2l0b3JfaWQiOiI0NzA0NDQyNTQzMzk3NDU1MjcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5eb129ece2f7a01387f9417dd51cf48314e39b16f888c018d23479b3e8a4f254 |
| hovercard-subject-tag | issue:834943506 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/1420/issue_layout |
| twitter:image | https://opengraph.githubassets.com/02fc607999e057fc80c3e45be74b366dfc54a3edb5c0ed82ef2bbf6f531dc84c/pythonnet/pythonnet/issues/1420 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/02fc607999e057fc80c3e45be74b366dfc54a3edb5c0ed82ef2bbf6f531dc84c/pythonnet/pythonnet/issues/1420 |
| og:image:alt | Environment Pythonnet version: 2.5.1 Python version: Python 3.9.2 Operating System: MacOS .NET Runtime: 5.0.200 Details Related to the problem reported in #1414 I.e. classes-in-classes are not hand... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | koliyo |
| hostname | github.com |
| expected-hostname | github.com |
| None | bb43a7bc61aba1b91c3c5cf8e7d00342e1e77a0cfe55a141222dbd7f9782d26f |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 34817b01ad7cdf8b2beb35ea7b0e2a7609004eff |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width