vfdev-5: @sdesrozis please, add tests of the bug case
sdesrozis: > @sdesrozis please, add tests of the bug case

Sorry, I didn't commit the test !
vfdev-5: @sdesrozis flake is failed: https://travis-ci.org/github/pytorch/ignite/jobs/674841603#L238
diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py
index 83106dc5..b8a457f9 100644
--- a/ignite/engine/engine.py
+++ b/ignite/engine/engine.py
@@ -5,6 +5,7 @@ from collections.abc import Mapping
 import weakref
 import random
 import warnings
+import functools
 from typing import Union, Optional, Callable, Iterable, Iterator, Any, Tuple, List
 
 import torch
@@ -197,12 +198,14 @@ class Engine:
             if event_to_attr and e in event_to_attr:
                 State.event_to_attr[e] = event_to_attr[e]
 
-    @staticmethod
-    def _handler_wrapper(handler: Callable, event_name: Any, event_filter: Callable) -> Callable:
-        def wrapper(engine: Engine, *args, **kwargs) -> Any:
-            event = engine.state.get_event_attrib_value(event_name)
-            if event_filter(engine, event):
-                return handler(engine, *args, **kwargs)
+    def _handler_wrapper(self, handler: Callable, event_name: Any, event_filter: Callable) -> Callable:
+        # signature of the following wrapper will be inspected during registering to check if engine is necessary
+        # we have to build a wrapper with relevant signature : solution is functools.wrapsgit s
+        @functools.wraps(handler)
+        def wrapper(*args, **kwargs) -> Any:
+            event = self.state.get_event_attrib_value(event_name)
+            if event_filter(self, event):
+                return handler(*args, **kwargs)
 
         # setup input handler as parent to make has_event_handler work
         wrapper._parent = weakref.ref(handler)
@@ -261,7 +264,7 @@ class Engine:
             and event_name.filter != CallableEventWithFilter.default_event_filter
         ):
             event_filter = event_name.filter
-            handler = Engine._handler_wrapper(handler, event_name, event_filter)
+            handler = self._handler_wrapper(handler, event_name, event_filter)
 
         if event_name not in self._allowed_events:
             self.logger.error("attempt to add event handler to an invalid event %s.", event_name)

Handler without engine raises error on filtered events
## 🐛 Bug description

The following code does not work:
```python
import torch
print(torch.__version__)
import ignite
print(ignite.__version__)

from ignite.engine import Engine, Events, State

def f4():
    print("f4")

    
e = Engine(lambda e, b: None)
e.state = State(iteration=0, epoch=0, max_epochs=10)

e.add_event_handler(Events.ITERATION_COMPLETED(every=2), f4)
e.fire_event(Events.ITERATION_COMPLETED)
e.fire_event(Events.ITERATION_COMPLETED)
```
Output
```

TypeErrorTraceback (most recent call last)
<ipython-input-13-832efed482dd> in <module>
     14 
     15 e.add_event_handler(Events.ITERATION_COMPLETED(every=2), f4)
---> 16 e.fire_event(Events.ITERATION_COMPLETED)
     17 e.fire_event(Events.ITERATION_COMPLETED)

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in fire_event(self, event_name)
    414 
    415         """
--> 416         return self._fire_event(event_name)
    417 
    418     def terminate(self) -> None:

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in _fire_event(self, event_name, *event_args, **event_kwargs)
    391                 kwargs.update(event_kwargs)
    392                 first, others = ((args[0],), args[1:]) if (args and args[0] == self) else ((), args)
--> 393                 func(*first, *(event_args + others), **kwargs)
    394 
    395     def fire_event(self, event_name: Any) -> None:

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in wrapper(engine, *args, **kwargs)
    203             event = engine.state.get_event_attrib_value(event_name)
    204             if event_filter(engine, event):
--> 205                 return handler(engine, *args, **kwargs)
    206 
    207         # setup input handler as parent to make has_event_handler work

TypeError: f4() takes 0 positional arguments but 1 was given
```


## Environment

 - PyTorch Version (e.g., 1.4): 1.6.0.dev20200411+cu101
 - Ignite Version (e.g., 0.3.0): 0.4.0.dev20200412


@sdesrozis could you please take a look. Thanks !
diff --git a/tests/ignite/engine/test_custom_events.py b/tests/ignite/engine/test_custom_events.py
index 42098a2c..33ef860f 100644
--- a/tests/ignite/engine/test_custom_events.py
+++ b/tests/ignite/engine/test_custom_events.py
@@ -229,11 +229,20 @@ def _test_every_event_filter_with_engine(device="cpu"):
             assert counter_every[0] == getattr(engine.state, event_attr)
             num_calls[0] += 1
 
+        @engine.on(event_name(every=every))
+        def assert_every_no_engine():
+            assert getattr(engine.state, event_attr) % every == 0
+            assert counter_every[0] == getattr(engine.state, event_attr)
+
         @engine.on(event_name)
         def assert_(engine):
             counter[0] += 1
             assert getattr(engine.state, event_attr) == counter[0]
 
+        @engine.on(event_name)
+        def assert_no_engine():
+            assert getattr(engine.state, event_attr) == counter[0]
+
         engine.run(data, max_epochs=5)
 
         assert num_calls[0] == true_num_calls

pip install -e .
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc pip install torch torchvision -f https://download.pytorch.org/whl/cpu/torch_stable.html -U
3.7
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
charset-normalizer==3.4.1
exceptiongroup==1.2.2
idna==3.10
importlib-metadata==6.7.0
iniconfig==2.0.0
numpy==1.21.6
packaging==24.0
Pillow==9.5.0
pluggy==1.2.0
pytest==7.4.4
-e git+https://github.com/pytorch/ignite.git@d62c4e930c5400905ddda4cfa69c1897e2e7ef40#egg=pytorch_ignite
requests==2.31.0
tomli==2.0.1
torch==1.13.1+cpu
torchvision==0.14.1+cpu
typing_extensions==4.7.1
urllib3==2.0.7
zipp==3.15.0

name: ignite
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - charset-normalizer==3.4.1
      - exceptiongroup==1.2.2
      - idna==3.10
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - numpy==1.21.6
      - packaging==24.0
      - pillow==9.5.0
      - pluggy==1.2.0
      - pytest==7.4.4
      - pytorch-ignite==0.4.0
      - requests==2.31.0
      - tomli==2.0.1
      - torch==1.13.1+cpu
      - torchvision==0.14.1+cpu
      - typing-extensions==4.7.1
      - urllib3==2.0.7
      - zipp==3.15.0
prefix: /opt/conda/envs/ignite

tests/ignite/engine/test_custom_events.py::test_every_event_filter_with_engine
tests/ignite/engine/test_custom_events.py::test_custom_events tests/ignite/engine/test_custom_events.py::test_custom_events_with_event_to_attr tests/ignite/engine/test_custom_events.py::test_custom_events_with_events_list tests/ignite/engine/test_custom_events.py::test_callable_events_with_wrong_inputs tests/ignite/engine/test_custom_events.py::test_callable_events tests/ignite/engine/test_custom_events.py::test_callable_events_every_eq_one tests/ignite/engine/test_custom_events.py::test_has_handler_on_callable_events tests/ignite/engine/test_custom_events.py::test_remove_event_handler_on_callable_events tests/ignite/engine/test_custom_events.py::test_once_event_filter_with_engine tests/ignite/engine/test_custom_events.py::test_custom_event_filter_with_engine tests/ignite/engine/test_custom_events.py::test_callable_event_bad_behaviour tests/ignite/engine/test_custom_events.py::test_custom_callable_events tests/ignite/engine/test_custom_events.py::test_custom_callable_events_with_engine tests/ignite/engine/test_custom_events.py::test_every_event_filter_with_engine_with_dataloader tests/ignite/engine/test_custom_events.py::test_event_list tests/ignite/engine/test_custom_events.py::test_list_of_events
ignite/engine/engine.py
ignite/engine/engine.py