René's URL Explorer Experiment


Title: A way to override Python.NET .NET <-> Python wrapping for custom classes · Issue #823 · pythonnet/pythonnet · GitHub

Open Graph Title: A way to override Python.NET .NET <-> Python wrapping for custom classes · Issue #823 · pythonnet/pythonnet

X Title: A way to override Python.NET .NET <-> Python wrapping for custom classes · Issue #823 · pythonnet/pythonnet

Description: Environment Pythonnet version: 2.3.0 Python version: 3.6 Operating System: Windows 10 x64 Details I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (see Gradient). Since I am automatically ge...

Open Graph Description: Environment Pythonnet version: 2.3.0 Python version: 3.6 Operating System: Windows 10 x64 Details I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (se...

X Description: Environment Pythonnet version: 2.3.0 Python version: 3.6 Operating System: Windows 10 x64 Details I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (se...

Opengraph URL: https://github.com/pythonnet/pythonnet/issues/823

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A way to override Python.NET .NET \u003c-\u003e Python wrapping for custom classes","articleBody":"### Environment\r\n\r\n-   Pythonnet version: 2.3.0\r\n-   Python version: 3.6\r\n-   Operating System: Windows 10 x64\r\n\r\n### Details\r\n\r\nI am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (see [Gradient](https://github.com/losttech/Gradient)).\r\n\r\nSince I am automatically generating .NET-side types for wrappers, I can make their methods to use my custom converter written in C# to convert function arguments to `PyObject` instances. And then for any result values, I can convert `PyObject` instances back into my wrapper types.\r\n\r\nProblem I am facing is when a user of my wrapper wants to inherit from one of the Python-derived classes, and override and/or extend its behavior.\r\n\r\nAn artificial sample:\r\n```csharp\r\n// this part of my library is auto-generated\r\n// represents tensorflow module\r\nclass tf {\r\n  static dynamic tf = Py.Import(\"tensorflow\");\r\n  // set_default_session - made up method\r\n  static void SetDefaultSession(Session session) =\u003e tf.set_default_session(session.underlyingSession);\r\n\r\n  // made up method\r\n  static void ComputeUsingDefaultSession() =\u003e tf.compute_using_default_session();\r\n}\r\n\r\n// represents tf.Tensor\r\nclass Tensor { PyObject underlyingTensor; }\r\n// represents tf.Session\r\nclass Session {\r\n  PyObject underlyingSession;\r\n  virtual object Run(Tensor tensor) {\r\n    var pyTensor = ConvertToPyObject(tensor); // simply does tensor.underlyingTensor here\r\n    dynamic pySession = this.underlyingSession;\r\n    var result = pySession.Run(pyTensor);\r\n    return ConvertFromPyObject(result); // wraps any returned object into one of generated classes\r\n  }\r\n}\r\n\r\n// this code is what user of my library wants to do:\r\nclass MyBetterSession : Session {\r\n  override object Run(Tensor tensor) {\r\n    ... here he writes custom code to run a Tensor ...\r\n  }\r\n}\r\n\r\n// an attempts to use the above (functions made up):\r\nvar simpleSession = new Session();\r\ntf.SetDefaultSession(simpleSession); // OK\r\ntf.ComputeUsingDefaultSession(); // SUCCEEDS\r\n\r\nvar betterSession = new MyBetterSession();\r\ntf.SetDefaultSession(betterSession); // OK\r\ntf.ComputeUsingDefaultSession(); // FAILS\r\n```\r\n\r\nThe last line will fail, because Python will attempt to call `MyBetterSession.Run` with Python's class `tf.Tensor`, but the method actually expects wrapped class `Tensor`. I need to somehow tell Python or rather Python.NET to invoke my `ConvertFromPyObject` on the argument, before trying to find a matching overload.\r\n\r\nI looked into Python.NET source, and the corresponding objects are `MethodBinding` and `MethodObject`, however, neither seem to provide any extensibility.\r\n\r\nNow I am considering several approaches to the problem, and I just wanted to discuss them with Python.NET team, as some of them involve modification of Python.NET.\r\n\r\n1. Do not change anything in Python.NET, and for every user class like `MyBetterSession` generate a pythonic wrapper with the same set of methods, but replacing all parameter and return types with `PyObject`. It would require one of the following:\r\n- `System.Reflection.Emit`, which is a very heavy dependency, and also not very pleasant to work with\r\n- Roslyn, which is much heavier, but moderately OK to work with\r\n\r\n2. I noticed, that while Python.NET supports representing any Python object as `dynamic`, it does not actually support passing `dynamic` objects (e.g. `IDynamicMetaObjectProvider` instances) back to Python.  I mean, it would pass them, but if Python would try to access a dynamic attribute, it would not attempt to call `TryGetMember`. I could potentially implement something similar to `ClassBase` specifically for wrapping `IDynamicMetaObjectProvider` instances. Then it is much easier to implement its members, that would simply wrap PyObject arguments before forwarding them to an instance of `MyBetterSession`.\r\n\r\n3. Have Python.NET directly expose some low-level interface, that would enable hooking into Python.NET's marshaling and, possibly, also method binding processes.","author":{"url":"https://github.com/lostmsu","@type":"Person","name":"lostmsu"},"datePublished":"2019-03-05T02:17:32.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/823/pythonnet/issues/823"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:1b261556-1b72-3b15-1e20-06d2fd59bf2d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD154:373B32:2C86014:3D90358:6971476A
html-safe-nonce0742c4bd5a807c9bef298f7aad0d7436aada2b4eeb74c1e1f29bc970d786e6de
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTU0OjM3M0IzMjoyQzg2MDE0OjNEOTAzNTg6Njk3MTQ3NkEiLCJ2aXNpdG9yX2lkIjoiNzkxNTcyODc5OTY0OTU4MDkwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac39544c4ea6d8c3136c3e990c54992a598ece35af2557148c3c81f15d750dd424
hovercard-subject-tagissue:417081611
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/823/issue_layout
twitter:imagehttps://opengraph.githubassets.com/04310dce5d9e5e1b7a58e5c5ff3c74d422a26d0bb136e7a9ae748b52822e44dc/pythonnet/pythonnet/issues/823
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/04310dce5d9e5e1b7a58e5c5ff3c74d422a26d0bb136e7a9ae748b52822e44dc/pythonnet/pythonnet/issues/823
og:image:altEnvironment Pythonnet version: 2.3.0 Python version: 3.6 Operating System: Windows 10 x64 Details I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (se...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamelostmsu
hostnamegithub.com
expected-hostnamegithub.com
None9bd14a55b65b030988a89787ed340c0f040258c3240af6941d85f740f21629a0
turbo-cache-controlno-preview
go-importgithub.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git
octolytics-dimension-user_id6050430
octolytics-dimension-user_loginpythonnet
octolytics-dimension-repository_id14748123
octolytics-dimension-repository_nwopythonnet/pythonnet
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id14748123
octolytics-dimension-repository_network_root_nwopythonnet/pythonnet
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release24a7109a3a584a05e29240fcc1ba60220deccdb8
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/pythonnet/pythonnet/issues/823#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fissues%2F823
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fissues%2F823
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=pythonnet%2Fpythonnet
Reloadhttps://github.com/pythonnet/pythonnet/issues/823
Reloadhttps://github.com/pythonnet/pythonnet/issues/823
Reloadhttps://github.com/pythonnet/pythonnet/issues/823
pythonnet https://github.com/pythonnet
pythonnethttps://github.com/pythonnet/pythonnet
Notifications https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Fork 770 https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Star 5.4k https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Code https://github.com/pythonnet/pythonnet
Issues 155 https://github.com/pythonnet/pythonnet/issues
Pull requests 18 https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects 0 https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/pythonnet/pythonnet/security
Please reload this pagehttps://github.com/pythonnet/pythonnet/issues/823
Insights https://github.com/pythonnet/pythonnet/pulse
Code https://github.com/pythonnet/pythonnet
Issues https://github.com/pythonnet/pythonnet/issues
Pull requests https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security https://github.com/pythonnet/pythonnet/security
Insights https://github.com/pythonnet/pythonnet/pulse
New issuehttps://github.com/login?return_to=https://github.com/pythonnet/pythonnet/issues/823
New issuehttps://github.com/login?return_to=https://github.com/pythonnet/pythonnet/issues/823
A way to override Python.NET .NET <-> Python wrapping for custom classeshttps://github.com/pythonnet/pythonnet/issues/823#top
https://github.com/lostmsu
https://github.com/lostmsu
lostmsuhttps://github.com/lostmsu
on Mar 5, 2019https://github.com/pythonnet/pythonnet/issues/823#issue-417081611
Gradienthttps://github.com/losttech/Gradient
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.