Repository: Textualize/rich (python)

Issue:
[BUG] Overriding `background_color` of `Syntax` doesn't include padding
- [x] I've checked [docs](https://rich.readthedocs.io/en/latest/introduction.html) and [closed issues](https://github.com/Textualize/rich/issues?q=is%3Aissue+is%3Aclosed) for possible solutions.
- [x] I can't find my issue in the [FAQ](https://github.com/Textualize/rich/blob/master/FAQ.md).

**Describe the bug**

See https://github.com/Textualize/textual/discussions/4227#discussioncomment-8634837 for context.

According to the [Syntax documentation](https://rich.readthedocs.io/en/stable/syntax.html#background-color):

> You can override the background color from the theme by supplying a background_color argument to the constructor.

However this doesn't seem to work as expected if the `Syntax` has padding:

![image](https://github.com/Textualize/rich/assets/101601846/65b375da-a8ca-4f6d-b208-4f2435f713f8)

```python
from rich.console import Console
from rich.syntax import Syntax

code = """\
def do_something():
    pass\
"""

console = Console()

syntax = Syntax(
    code,
    lexer="python",
    word_wrap=False,
    indent_guides=True,
    padding=(1, 2),
    theme="material",
    background_color="red",
)

console.print(syntax)
```

If you set the `background_color` to the special value "default", this produces a different unexpected result:

![image](https://github.com/Textualize/rich/assets/101601846/9a21cdf2-3617-4cb8-9e60-1891a8b7a67a)

**Platform**

Platform is Linux and tested in various terminal emulators.

```console
$ pip freeze | grep rich
rich==13.6.0
```

Pull request description:
fix(syntax): include padding with background color override
## Type of changes

- [x] Bug fix
- [ ] New feature
- [ ] Documentation / docstrings
- [ ] Tests
- [ ] Other

## Checklist

- [x] I've run the latest [black](https://github.com/psf/black) with default args on new code.
- [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
- [x] I've added tests for new code.
- [x] I accept that @willmcgugan may be pedantic in the code review.

## Description

Fixes #3295. I've tested manually but I'm afraid not sure how to add a proper test for this.

#### Before:

![image](https://github.com/Textualize/rich/assets/101601846/28475ac9-1dd3-4871-b267-85c0ca6ea65b)

#### After:

![image](https://github.com/Textualize/rich/assets/101601846/73a70acc-20dd-4aed-99b0-f60cb1bbb2c4)

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e153073c..9eff3e39 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,7 +5,6 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
-
 ## Unreleased
 
 ### Fixed
@@ -16,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Thread used in progress.track will exit if an exception occurs in a generator https://github.com/Textualize/rich/pull/3402
 - Progress track thread is now a daemon thread https://github.com/Textualize/rich/pull/3402
 - Fixed cached hash preservation upon clearing meta and links https://github.com/Textualize/rich/issues/2942
+- Fixed overriding the `background_color` of `Syntax` not including padding https://github.com/Textualize/rich/issues/3295
 
 ### Changed
 
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [13.7.1] - 2024-02-28
 
+
 ### Fixed
 
 - Updated the widths of some characters https://github.com/Textualize/rich/pull/3289
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index b107adc8..0ce5307a 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -20,6 +20,7 @@ The following people have contributed to the development of Rich:
 - [Aryaz Eghbali](https://github.com/AryazE)
 - [Oleksis Fraga](https://github.com/oleksis)
 - [Andy Gimblett](https://github.com/gimbo)
+- [Tom Gooding](https://github.com/TomJGooding)
 - [Michał Górny](https://github.com/mgorny)
 - [Nok Lam Chan](https://github.com/noklam)
 - [Leron Gray](https://github.com/daddycocoaman)
diff --git a/rich/syntax.py b/rich/syntax.py
index f3ceff75..4da6c3b7 100644
--- a/rich/syntax.py
+++ b/rich/syntax.py
@@ -619,9 +619,7 @@ class Syntax(JupyterMixin):
     ) -> RenderResult:
         segments = Segments(self._get_syntax(console, options))
         if self.padding:
-            yield Padding(
-                segments, style=self._theme.get_background_style(), pad=self.padding
-            )
+            yield Padding(segments, style=self._get_base_style(), pad=self.padding)
         else:
             yield segments
 


Test patch:
diff --git a/tests/test_syntax.py b/tests/test_syntax.py
index 37cc293e..bbd4c7a5 100644
--- a/tests/test_syntax.py
+++ b/tests/test_syntax.py
@@ -406,6 +406,23 @@ def test_syntax_measure():
     assert code.__rich_measure__(console, console.options) == Measurement(3, 24)
 
 
+def test_background_color_override_includes_padding():
+    """Regression test for https://github.com/Textualize/rich/issues/3295"""
+
+    syntax = Syntax(
+        "x = 1",
+        lexer="python",
+        padding=(1, 3),
+        background_color="red",
+    )
+    result = render(syntax)
+    print(repr(result))
+    assert (
+        result
+        == "\x1b[41m                                                                                                    \x1b[0m\n\x1b[41m   \x1b[0m\x1b[38;2;248;248;242;41mx\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;255;70;137;41m=\x1b[0m\x1b[38;2;248;248;242;41m \x1b[0m\x1b[38;2;174;129;255;41m1\x1b[0m\x1b[41m                                                                                         \x1b[0m\x1b[41m   \x1b[0m\n\x1b[41m                                                                                                    \x1b[0m\n"
+    )
+
+
 if __name__ == "__main__":
     syntax = Panel.fit(
         Syntax(