Repository: sympy/sympy

Issue:
Interval(0, 1).union(FiniteSet(1, x)) drops 'x'
Somehow `x` is discarded in the second example:
```
In [1]: Interval(0, 1).union(FiniteSet(2, x))
Out[1]: [0, 1] ∪ {2, x}                                                                                                        
In [2]: Interval(0, 1).union(FiniteSet(1, x))
Out[2]: [0, 1]
```


Discussion:

Patch:
diff --git a/sympy/sets/handlers/union.py b/sympy/sets/handlers/union.py
index a25dc561da..3599b2bb6e 100644
--- a/sympy/sets/handlers/union.py
+++ b/sympy/sets/handlers/union.py
@@ -135,7 +135,7 @@ def union_sets(a, b): # noqa:F811
     # If `b` set contains one of my elements, remove it from `a`
     if any(b.contains(x) == True for x in a):
         return set((
-            FiniteSet(*[x for x in a if not b.contains(x)]), b))
+            FiniteSet(*[x for x in a if b.contains(x) != True]), b))
     return None
 
 @dispatch(Set, Set)


Test patch:
diff --git a/sympy/sets/tests/test_sets.py b/sympy/sets/tests/test_sets.py
index 0afe45e465..63b53a21fa 100644
--- a/sympy/sets/tests/test_sets.py
+++ b/sympy/sets/tests/test_sets.py
@@ -186,6 +186,11 @@ def test_union():
 
     assert Union(Interval(0, 1), *[FiniteSet(1.0/n) for n in range(1, 10)]) == \
         Interval(0, 1)
+    # issue #18241:
+    x = Symbol('x')
+    assert Union(Interval(0, 1), FiniteSet(1, x)) == Union(
+        Interval(0, 1), FiniteSet(x))
+    assert unchanged(Union, Interval(0, 1), FiniteSet(2, x))
 
     assert Interval(1, 2).union(Interval(2, 3)) == \
         Interval(1, 2) + Interval(2, 3)