Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix h.Vector[0] : TypeError: type 'hoc.Vector' is not subscriptable #2862

Merged
merged 19 commits into from
Sep 16, 2024

Conversation

nrnhines
Copy link
Member

@nrnhines nrnhines commented Apr 30, 2024

Closes #2860

PyType_FromMetaClass resolves the issue but requires python version >= 3.12. The function is wrapped by
nrn_type_from_metaclass in nrn_metaclass.cpp and for python version < 3.12 it contains a "cursed" implementation that seems to work but required some tweaking on my part.

[x] In particular <3.12 seems to be missing the __module__ field. E.g we would like to see

$ python
Python 3.12.4 (main, Aug  5 2024, 09:04:16) [GCC 13.2.0] on linux
>>> from neuron import h
>>> v = h.Vector()
>>> type(v).__module__
'hoc'

Otherwise, for the most part, things are looking pretty good...

$ python
Python 3.10.14 (main, Jul 14 2024, 15:28:18) [GCC 13.2.0] on linux
>>> from neuron import h
>>> v = h.Vector(range(5))
>>> v.printf()
0	1	2	3	4	

5
>>> type(v)
<class 'hoc.Vector'>
>>> type(type(v))
<class 'hoc.HocClass'>
>>> type(type(type(v)))
<class 'type'>
>>> v
Vector[0]
>>> h.Vector[0][2]
2.0
>>> type(v)[0] == v
True
>>> type(v).mro()
[<class 'hoc.Vector'>, <class 'hoc.HocObject'>, <class 'object'>]

[x] Perhaps test/hoctests/tests/test_hoc_class_instance.py should be augmented with more extensive tests.

[x] At the moment, the implementation of hocclass_getitem is not very efficient. It may be worthwhile to give that object a Symbol* field (filled in at construction time) for immediate searching of sym->u.ctemplate->olist

[x] It is not clear at the moment that hocobject_type needs the custom_hocclass metaclass.

[ ] Should user defined hoc classes participate in this PR? Their type is old style...

>>> h('''
... begintemplate A
... endtemplate A
... ''')
True
>>> a = h.A()
>>> a
A[0]
>>> a == h.A[0]
True
>>> type(a)
<class 'hoc.HocObject'>

[ ] The nrnpy_hoc.cpp implementation is somewhat unclean. Error handling is inconsistent in nrnpy_hoc()

@nrnhines nrnhines marked this pull request as draft April 30, 2024 11:50
Copy link

✔️ baba6fe -> Azure artifacts URL

@bbpbuildbot

This comment has been minimized.

Copy link

sonarcloud bot commented May 2, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@bbpbuildbot

This comment has been minimized.

Copy link

✔️ c37983f -> Azure artifacts URL

Copy link

sonarcloud bot commented Jul 18, 2024

Copy link

✔️ 0a7baa2 -> Azure artifacts URL

@bbpbuildbot

This comment has been minimized.

Copy link

✔️ 8178114 -> Azure artifacts URL

@bbpbuildbot

This comment has been minimized.

Supports h.File[index], etc via hocclass_getitem
@bbpbuildbot

This comment has been minimized.

@bbpbuildbot

This comment has been minimized.

Copy link

✔️ eb3088d -> Azure artifacts URL

@bbpbuildbot

This comment has been minimized.

Copy link

codecov bot commented Sep 5, 2024

Codecov Report

Attention: Patch coverage is 76.74419% with 20 lines in your changes missing coverage. Please review.

Project coverage is 67.27%. Comparing base (8e3de33) to head (47212cc).

Files with missing lines Patch % Lines
src/nrnpython/nrnpy_hoc.cpp 65.95% 16 Missing ⚠️
src/nrnpython/nrn_metaclass.cpp 89.74% 4 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2862   +/-   ##
=======================================
  Coverage   67.26%   67.27%           
=======================================
  Files         571      572    +1     
  Lines      104867   104943   +76     
=======================================
+ Hits        70542    70596   +54     
- Misses      34325    34347   +22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nrnhines nrnhines marked this pull request as ready for review September 5, 2024 15:56
Copy link

✔️ c1d7fae -> Azure artifacts URL

@bbpbuildbot

This comment has been minimized.

Copy link
Collaborator

@1uc 1uc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small comment:

  • the PR mixes NULL and nullptr.

src/nrnpython/nrnpy_hoc.cpp Outdated Show resolved Hide resolved
src/nrnpython/nrnpy_hoc.cpp Outdated Show resolved Hide resolved
src/nrnpython/nrnpy_hoc.cpp Outdated Show resolved Hide resolved
@pramodk pramodk added this to the Release v9.0 milestone Sep 9, 2024
Copy link

✔️ 47212cc -> Azure artifacts URL

Copy link

✔️ 8501922 -> Azure artifacts URL

Copy link

sonarcloud bot commented Sep 16, 2024

Copy link

✔️ 7a5fe7e -> Azure artifacts URL

@nrnhines nrnhines merged commit e67effd into master Sep 16, 2024
35 of 36 checks passed
@nrnhines nrnhines deleted the hines/hoc-class-instance-syntax branch September 16, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NEURON v9 python syntax does not allow use of template instance name
4 participants