Skip to content

Still cannot create Nullable<T>(None) #1693

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

Closed
mrwizard82d1 opened this issue Feb 9, 2022 · 3 comments
Closed

Still cannot create Nullable<T>(None) #1693

mrwizard82d1 opened this issue Feb 9, 2022 · 3 comments

Comments

@mrwizard82d1
Copy link

mrwizard82d1 commented Feb 9, 2022

Environment

  • Pythonnet version: 2.5.2
  • Python version: 3.8.7
  • Operating System: Windows 10
  • .NET Runtime:
    Runtime Environment:
    OS Name: Windows
    OS Version: 10.0.19044
    OS Platform: Windows
    RID: win10-x64
    Base Path: C:\Program Files\dotnet\sdk\6.0.101\

Details

  • Describe what you were trying to get done.

I was trying to invoke a C# function with an argument of type Nullable. Python.NET reported that it could find no function matching the arguments. Eventually, I tried to test the fix for issue #460.

Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.28.0 -- An enhanced Interactive Python. Type '?' for help.

[ins] In [1]: import clr

[ins] In [2]: from System import (UInt32, Nullable)

[ins] In [3]: Nullable[UInt32](3)
Out[3]: <System.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] object at 0x0000020A6AF106D0>

[ins] In [4]: Nullable[UInt32](None)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-b1659b301f87> in <module>
----> 1 Nullable[UInt32](None)

TypeError: No constructor matches given arguments: (<class 'NoneType'>)
  • If there was a crash, please include the traceback here.

I also tried testing against pythonnet 3.0.0.a2. I see the following stack trace.

Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)]                                                             
Type 'copyright', 'credits' or 'license' for more information                                                                                     
IPython 8.0.1 -- An enhanced Interactive Python. Type '?' for help.                                                                               
                                                                                                                                                  
[ins] In [1]: import clr                                                                                                                          
                                                                                                                                                  
[ins] In [2]: from System import DateTime, Double, Guid, Nullable, String, UInt32                                                                 
                                                                                                                                                  
[ins] In [3]: Nullable[Double](None)                                                                                                              
Python.Runtime.PythonException: 'NoneType' value cannot be converted to System.Double                                                             
                                                                                                                                                  
                                                                                                                                                  
The above exception was the direct cause of the following exception:                                                                              
                                                                                                                                                  
System.ArgumentException: 'NoneType' value cannot be converted to System.Double in method Void .ctor(Double) ---> Python.Runtime.PythonException: 
'NoneType' value cannot be converted to System.Double                                                                                             
                                                                                                                                                  
   --- End of inner exception stack trace ---                                                                                                     
                                                                                                                                                  
The above exception was the direct cause of the following exception:                                                                              
                                                                                                                                                  
System.AggregateException: One or more errors occurred. ---> System.ArgumentException: 'NoneType' value cannot be converted to System.Double in me
thod Void .ctor(Double) ---> Python.Runtime.PythonException: 'NoneType' value cannot be converted to System.Double                                
                                                                                                                                                  
                                                                                                                                                  
   --- End of inner exception stack trace ---                                                                                                     
   --- End of inner exception stack trace ---                                                                                                     
---> (Inner Exception #0) System.ArgumentException: 'NoneType' value cannot be converted to System.Double in method Void .ctor(Double) ---> Python
.Runtime.PythonException: 'NoneType' value cannot be converted to System.Double                                                                   
                                                                                                                                                  
   --- End of inner exception stack trace ---<---                                                                                                 
                                                                                                                                                  
                                                                                                                                                  
The above exception was the direct cause of the following exception:                                                                              
                                                                                                                                                  
Traceback (most recent call last):                                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 3251, in run_code                                                                                                                              
                                                                                                                                                  
    exec(code_obj, self.user_global_ns, self.user_ns)                                                                                             
  File "<ipython-input-3-3b78c20f70fb>", line 1, in <module>                                                                                      
    Nullable[Double](None)                                                                                                                        
TypeError: No constructor matches given arguments: (<class 'NoneType'>)                                                                           
                                                                                                                                                  
During handling of the above exception, another exception occurred:                                                                               
                                                                                                                                                  
Traceback (most recent call last):                                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 1934, in showtraceback                                                                                                                         
                                                                                                                                                  
    stb = value._render_traceback_()                                                                                                              
AttributeError: 'TypeError' object has no attribute '_render_traceback_'                                                                          
                                                                                                                                                  
During handling of the above exception, another exception occurred:                                                                               
                                                                                                                                                  
Traceback (most recent call last):                                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 3191, in run_ast_nodes                                                                                                                         
                                                                                                                                                  
    if await self.run_code(code, result, async_=asy):                                                                                             
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 3268, in run_code                                                                                                                              
                                                                                                                                                  
    self.showtraceback(running_compiled_code=True)                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 1936, in showtraceback                                                                                                                         
                                                                                                                                                  
    stb = self.InteractiveTB.structured_traceback(etype,                                                                                          
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 1105, 
in structured_traceback                                                                                                                           
                                                                                                                                                  
    return FormattedTB.structured_traceback(                                                                                                      
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 999, i
n structured_traceback                                                                                                                            
                                                                                                                                                  
    return VerboseTB.structured_traceback(                                                                                                        
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 871, i
n structured_traceback                                                                                                                            
                                                                                                                                                  
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,                                                
                                                                                                                                                  
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 775, i
n format_exception_as_a_whole                                                                                                                     
                                                                                                                                                  
    assert etb is not None                                                                                                                        
AssertionError                                                                                                                                    
                                                                                                                                                  
During handling of the above exception, another exception occurred:                                                                               
                                                                                                                                                  
Traceback (most recent call last):                                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 1934, in showtraceback                                                                                                                         
                                                                                                                                                  
    stb = value._render_traceback_()                                                                                                              
AttributeError: 'AssertionError' object has no attribute '_render_traceback_'                                                                     
                                                                                                                                                  
During handling of the above exception, another exception occurred:                                                                               
                                                                                                                                                  
Traceback (most recent call last):                                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 2814, in _run_cell                                                                                                                             
                                                                                                                                                  
    return runner(coro)                                                                                                                           
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\async_helpers.py", line 
129, in _pseudo_sync_runner                                                                                                                       
                                                                                                                                                  
    coro.send(None)                                                                                                                               
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 3012, in run_cell_async                                                                                                                        
                                                                                                                                                  
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,                                                                               
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 3210, in run_ast_nodes                                                                                                                         
                                                                                                                                                  
    self.showtraceback()                                                                                                                          
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 1936, in showtraceback                                                                                                                         
                                                                                                                                                  
    stb = self.InteractiveTB.structured_traceback(etype,                                                                                          
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 1105, 
in structured_traceback                                                                                                                           
                                                                                                                                                  
    return FormattedTB.structured_traceback(                                                                                                      
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 999, i
n structured_traceback                                                                                                                            
                                                                                                                                                  
    return VerboseTB.structured_traceback(                                                                                                        
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 871, i
n structured_traceback                                                                                                                            
                                                                                                                                                  
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,                                                
                                                                                                                                                  
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 775, i
n format_exception_as_a_whole                                                                                                                     
                                                                                                                                                  
    assert etb is not None                                                                                                                        
AssertionError                                                                                                                                    
                                                                                                                                                  
During handling of the above exception, another exception occurred:                                                                               
                                                                                                                                                  
Traceback (most recent call last):                                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 1934, in showtraceback                                                                                                                         
                                                                                                                                                  
    stb = value._render_traceback_()                                                                                                              
AttributeError: 'AssertionError' object has no attribute '_render_traceback_'                                                                     
                                                                                                                                                  
During handling of the above exception, another exception occurred:                                                                               
                                                                                                                                                  
Traceback (most recent call last):                                                                                                                
  File "C:\Users\larry.jones\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main                               
                                                                                                                                                  
    return _run_code(code, main_globals, None,                                                                                                    
  File "C:\Users\larry.jones\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code                                          
                                                                                                                                                  
    exec(code, run_globals)                                                                                                                       
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\Scripts\ipython.exe\__main__.py", line 7, in <module>  
                                                                                                                                                  
    sys.exit(start_ipython())                                                                                                                     
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\__init__.py", line 123, in st
art_ipython                                                                                                                                       
    return launch_new_instance(argv=argv, **kwargs)                                                                                               
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\traitlets\config\application.py", lin
e 846, in launch_instance                                                                                                                         
                                                                                                                                                  
    app.start()                                                                                                                                   
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\terminal\ipapp.py", line 316,
 in start                                                                                                                                         
    self.shell.mainloop()                                                                                                                         
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\terminal\interactiveshell.py"
, line 611, in mainloop                                                                                                                           
                                                                                                                                                  
    self.interact()                                                                                                                               
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\terminal\interactiveshell.py"
, line 604, in interact                                                                                                                           
                                                                                                                                                  
    self.run_cell(code, store_history=True)                                                                                                       
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 2768, in run_cell                                                                                                                              
                                                                                                                                                  
    result = self._run_cell(                                                                                                                      
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 2819, in _run_cell                                                                                                                             
                                                                                                                                                  
    self.showtraceback(running_compiled_code=True)                                                                                                
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\interactiveshell.py", li
ne 1936, in showtraceback                                                                                                                         
                                                                                                                                                  
    stb = self.InteractiveTB.structured_traceback(etype,                                                                                          
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 1105, 
in structured_traceback                                                                                                                           
                                                                                                                                                  
    return FormattedTB.structured_traceback(                                                                                                      
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 999, i
n structured_traceback                                                                                                                            
                                                                                                                                                  
    return VerboseTB.structured_traceback(                                                                                                        
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 871, i
n structured_traceback                                                                                                                            
                                                                                                                                                  
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,                                                
                                                                                                                                                  
  File "C:\Users\larry.jones\AppData\Local\pypoetry\Cache\virtualenvs\libpy-QVeQv1xn-py3.8\lib\site-packages\IPython\core\ultratb.py", line 775, i
n format_exception_as_a_whole                                                                                                                     
                                                                                                                                                  
    assert etb is not None                                                                                                                        
AssertionError                                                                                                                                    
                                                                                                                                                  
If you suspect this is an IPython 8.0.1 bug, please report it at:                                                                                 
    https://github.com/ipython/ipython/issues                                                                                                     
or send an email to the mailing list at ipython-dev@python.org                                                                                    
                                                                                                                                                  
You can print a more detailed traceback right now with "%tb", or use "%debug"                                                                     
to interactively debug it.                                                                                                                        
                                                                                                                                                  
Extra-detailed tracebacks for bug-reporting purposes can be enabled via:                                                                          
    %config Application.verbose_crash=True                                                                                                        
                                                                                                                                                  

@lostmsu
Copy link
Member

lostmsu commented Feb 9, 2022

Why do you need an instance of Nullable<T> with value null? Why can't you use None?

@mrwizard82d1
Copy link
Author

mrwizard82d1 commented Feb 9, 2022

Mea culpa.

Based on the comment, I went back to my original code and successfully passed None for an argument of type Nullable<T>. Additionally, I opened up LinqPad and verified that C# does not allow an expression like var foo = Nullable<Int32>)(null).

I am so sorry for wasting everyone's time.

@mrwizard82d1
Copy link
Author

Closing issue.

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

No branches or pull requests

2 participants