code: def test_setattr() -> None:
      class A:
          x = 1
  
      monkeypatch = MonkeyPatch()
      pytest.raises(
          AttributeError, monkeypatch.setattr, A, "notexists", 2
      )
      monkeypatch.setattr(A, "y", 2, raising=False)
      assert A.y == 2  # type: ignore
      monkeypatch.undo()
      assert not hasattr(A, "y")
  
      monkeypatch = MonkeyPatch()
      monkeypatch.setattr(A, "x", 2)
      assert A.x == 2
      monkeypatch.setattr(A, "x", 3)
      assert A.x == 3
      monkeypatch.undo()
      assert A.x == 1
  
      A.x = 5
      monkeypatch.undo()  # double-undo makes no modification
      assert A.x == 5
  
      with pytest.raises(TypeError):
          monkeypatch.setattr(A, "y")
  
imports: import os
  import re
  import sys
  import textwrap
  from pathlib import Path
  from typing import Dict
  from typing import Generator
  from typing import Type
  import pytest
  from _pytest.monkeypatch import MonkeyPatch
  from _pytest.pytester import Pytester
label_desc: Use image node_id 2 for calling a global function with example usage: test_setattr() without return types