ivankorobkov / python-inject

Python dependency injection
Apache License 2.0
671 stars 77 forks source link

support_pytest_issues #57

Closed XFrankly closed 4 years ago

XFrankly commented 4 years ago

I am env at windows 10 and python3.6.8, ================================================== test session starts ================================================== platform win32 -- Python 3.6.8, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
cachedir: .pytest_cache

I want to run the test "test_injector,py" as python -m unittest test_injector Its work normal as the follow info: test_injector.py:48: DeprecationWarning: Please use assertRaisesRegex instead. injector.get_instance, int) ...

Ran 6 tests in 0.002s

OK

and then, I want to run the module with pytest

pytest test_injector.py , that was failure with this information init.py:3: in import inject ..\inject__init__.py:88: in from typing import Any, Callable, Dict, ForwardRef, Generic, Hashable, Optional, Type, TypeVar, \ E ImportError: cannot import name 'ForwardRef' ================================================ short test summary info ================================================ ERROR test_injector.py !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! =================================================== 1 error in 0.08s

ivankorobkov commented 4 years ago

Hi!

Do you use inject from the master branch? Master is unstable. Try to switch to the stable version and see if the error still occurs. See https://github.com/ivankorobkov/python-inject/releases/tag/v4.1.2

XFrankly commented 4 years ago

Hi!

Do you use inject from the master branch? Master is unstable. Try to switch to the stable version and see if the error still occurs. See https://github.com/ivankorobkov/python-inject/releases/tag/v4.1.2

Great, the releases version 4.1.2 much better than master branch there only have two warning like this: test/test_injector.py::TestInjector::test_runtime_binding__not_callable ..\test\test_injector.py:42: DeprecationWarning: Please use assertRaisesRegex instead. injector.get_instance, 123)

I am trying to find the really reason for Warning.

ivankorobkov commented 4 years ago

Please, check the master version.

XFrankly commented 4 years ago

Please, check the master version.

Yes, I have checked, there are moved the test_future to dir test37, It was can be fixed the prob in another way. if you williing I thought thats can use unittest func @unittest.skipIf() and py version to fixed , and pytest warning is can be fixed quickly also.

XFrankly commented 4 years ago

Please, check the master version.

Yes, I have checked, there are moved the test_future to dir test37, It was can be fixed the prob in another way. if you williing I thought thats can use unittest func @unittest.skipIf() and py version to fixed , and pytest warning is can be fixed quickly also.

after fixed, we can use the test dir by this way: pytest test and recive info like this: ================================================== test session starts ================================================== platform win32 -- Python 3.6.8, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: ../inject collected 44 items

test\test_attr.py .. [ 4%] test\test_autoparams.py ......... [ 25%] test\test_binder.py ....... [ 40%] test\test_functional.py . [ 43%] test\test_future.py s [ 45%] test\test_inject_configuration.py ......... [ 65%] test\test_injector.py ...... [ 79%] test\test_instance.py . [ 81%] test\test_param.py .. [ 86%] test\test_params.py ...... [100%]

============================================= 43 passed, 1 skipped in 0.17s =============================================

ivankorobkov commented 4 years ago

Hi,

Thank you proposing a solution @unittest.skipIf. Unfortunately, the problem is not with the unittest itself but with an import from __future__ import annotations. This enables new annotation processing algorithm and it must be the first import in a python file. So I had to move it to another directory and split tests into python3.6+ and python3.7+.

I have fixed the warning you posted above, changed self.assertRaisesRegexp into self.assertRaisesRegex in test_injector.py. Mind the lack of p at the end.

I'm still waiting for other people feedback and hope to get it today. If everything is OK, then I'll release a new version later today.

XFrankly commented 4 years ago

Hi,

Thank you proposing a solution @unittest.skipIf. Unfortunately, the problem is not with the unittest itself but with an import from __future__ import annotations. This enables new annotation processing algorithm and it must be the first import in a python file. So I had to move it to another directory and split tests into python3.6+ and python3.7+.

I have fixed the warning you posted above, changed self.assertRaisesRegexp into self.assertRaisesRegex in test_injector.py. Mind the lack of p at the end.

I'm still waiting for other people feedback and hope to get it today. If everything is OK, then I'll release a new version later today.

Yes, you are right "future import" This enables new annotation processing algorithm and it must be the first import in a python file, could be due to import error, this can be fixed by a specialized import module do that, and then from import_module import * fixed the import error. so, we can use unittest.Skipif.... Help to that solution can be helpful.

ivankorobkov commented 4 years ago

I've released 4.2.0 https://pypi.org/project/Inject/